c# - Unity-Why is Z ax changing when I move an object on X ax? -


I am creating a game in which I will have a cube but when I move my cube on the X ax, the Z ax will change is. I have also blocked X, Y, Z rotation; MI code:

  using UnityGen; Using System.Collections; Public class qubcintrol: monobehavier {private vector 3 input; Zero Update () {if (Input.GetKey (KeyCode.D)) {input = new vector3 (25, 0, 0); Rigidbody.AddForce (input); } If (input gateke (keycode.A)) {input = new vector 3 (-25, 0, 0); Rigidbody.AddForce (input); } If (Input Gateke (keycode.)) {Input = new vector 3 (0, 0, 25); Rigidbody.AddForce (input); } If (input .gateke (keycode s)) {input = new vector 3 (0, 0, -25); Rigidbody.AddForce (input); }}}}  

It seems that you are implementing the movement in the world space Are there. If you want to apply it to object space, you will rotate the generating vector while converting objects. Something like this:

  vector 3 input = new vector 3 (25,0,0); Input = this.transform.rotation * input; Rigidbody.AddForce (input);  

In addition, some things to make your life easier:

  • To install things after installing / after items in input In order to configure, you can reduce your life.

  • Use Vector3.up / Vector3.left / Vector3.forward etc. and multiply it by a scalar. When you want to multiply, things look more intuitive.


Comments