I have a class with this method
rotire.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using System.Collections;
namespace WpfApplication3
{
class rotire:MonoBehaviour
{
float speed = 10f;
public void rotiree()
{
transform.Rotate(new Vector3(15,40,45)*speed,Time.deltaTime);
}
}
}
I want use this method to rotate my cube made in XAML.
Unfortunately it not work and I think my code is incorectly.
Please, can someone help me whit an ideea, what i should write.
Window1,cs
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
rotire rot = new rotire();
rot.rotiree();
mycube.Transform = rot;
}
I belive the last line of code is wrong, because i receive this error
"Cannot implicitly convert type 'WpfApplication3.rotire' to 'System.Windows.Media.Media3D.Transform3D'"
That last line should be the following:
mycube.Transform = rot.transform;
Related
im trying to write a script that makes the temperature of the light whatever number is set in the float
i need this for later reference when i add a slider or selectable options that determine the temperature of the light
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class lightchange : MonoBehaviour
{
public float Temp = 2000f;
private void pp()
{
Light lightcomp = GetComponent<Light>();
lightcomp.colorTemperature = Temp;
}
}
this is my code but it doesnt seem to work and this is all i could find online to edit componentswhat im trying to change
For school, I have to create a calculator where you enter the radius of a circle and it calculates the circumference and the area.
I tried coding it and everything I tried just won't work. It kept giving me errors along the lines of it having difficulty converting the textbox into a double, and other errors. This is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Circumference
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
txtCircumference = Math.PI * Math.Pow(txtRadius, 2);
txtArea = 2 * Math.PI * txtRadius;
}
}
}
I'm used to coding with python so C# is kinda out of my comfort zone.
You may be new to C# but the most glaring issue is with WinForm controls. My answer assumes that txtRadius, txtCircumference, and txtArea are all TextBox controls.
Note I am writing this freeform and not in a VS editor so I may have errors.
private void btnCalculate_Click(object sender, EventArgs e)
{
if (!double.TryParse(txtRadius.Text, out double radius))
{
throw new Exception("Radius is a not a valid double.");
}
txtCircumference.Text = (Math.PI * radius * radius).ToString();
txtArea.Text = (2.0 * Math.PI * radius).ToString();
}
Observe that each TextBox has a .Text string property. For me to interchange a double with a string requires me to parse the string into a double, or else use ToString() to write the double as a string.
And here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneManager : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown("space")) {
gameObject.GetComponent<Animator>().SetBool("Gone", true);
}
if (gameObject.transform.Scale.x == 0) {
UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync("Stage1");
UnityEngine.SceneManagement.SceneManager.LoadSceneAsync("Stage2");
}
}
}
The line :if (gameObject.transform.Scale.x == 0) is the issue,
transform.Scale does not exist. You want to use transform.localScale (for local scale) or transform.lossyScale (for the total scale of the transform including parent's).
Scale doesn't exist as a property of a Transform instance, but you can use localScale or lossyScale, depending of your needs. More informations about can be found in the linked documentation.
I want to draw a cross into a picturebox. In the next step I want to move the cross based on my Data.
Which is the Thumbsticks position. I want it to appear similar to the figure below.
Here is my Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SlimDX;
using SlimDX.XInput;
namespace Controller_Test
{
public partial class Form1 : Form
{
GamepadState Controller = new GamepadState(UserIndex.One);
Graphics drawArea;
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
drawArea = pictureBox1.CreateGraphics();
}
private void timer1_Tick(object sender, EventArgs e)
{
Controller.Update();
var LeftStick = Controller.LeftStick;
float LeftStickX = LeftStick.Position.X;
float LeftStickY = LeftStick.Position.Y;
Pen blackPen = new Pen(Color.Black);
// horizontal Line
drawArea.DrawLine(blackPen, x1+LeftStickX, y1-LeftStickY, x2+LeftStickX, y2-LeftStickY);
// vertical Line
drawArea.DrawLine(blackPen, x1+LeftStickX, y1-LeftStickY, x2+LeftStickX, y2-LeftStickY);
}
}
public class GamepadState
{Get Postiton......}
}
My problem now is, if I move the Thumbstick every time a new cross is drawn but the old one is still in the
Picturebox like in the figure below. If i use the pictureBox1.Invalidate():/pictureBox1.Refresh(); Funtcion
the cross becomes invisible and is not displayed anymore. The interval of the timer is on 1.
What could i change in order to get the code to work?
So I am following the book "C# Game Programming - For Serious Game Creation" and I'm at a part in the book that has you create OpenGL form with Tao and change the color from black to red.
Here is my code which is exactly the same as in the book
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tao.OpenGl;
namespace GameLoop
{
public partial class Form1 : Form
{
FastLoop _fastLoop;
bool _fullscreen = false;
public Form1()
{
_fastLoop = new FastLoop(GameLoop);
InitializeComponent();
_openGLControl.InitializeContexts();
if (_fullscreen)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
}
}
void GameLoop(double elapsedTime)
{
Gl.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
Gl.glFinish();
_openGLControl.Refresh();
}
}
}
I am not getting any errors at all, it just simply stays black. What are the possible reasons for why this could be happening?
code for other game loop
static void GameLoop(double elapsedTime)
{
// GameCode goes here
// GetInput
// Process
// Render
System.Console.WriteLine("Loop");
}
Thanks to Ali Kazmi and Matthew I figured it out. by puting the code into public Form1() It worked and I was able to conclude that the method was not updating and Im very new to C# I think it was because the other game loop was not letting it get that far. I removed it and it all works fine.