- Home /
z axis doesn't follow the rotation of its object
hello guys, strange problem this time. in myscene I want to pilot an airplane but I get some issues doing that. The problem is this:
when I rotate the airplane, its z axis (forward direction) don't rotate with it, as shown in the image below.
with D button and Abutton I rotate clockwise and counterclockwise, but Z are always facing z world. This is bad expecially when I add a force to my airplane, trying to drive it where I want. The result is that I rotate the plane but it continues to go straight, aiming the z world axis and not its front nose.
here u are the script:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public float m_speed = 4.0f;
public Rigidbody m_airplane = null; //I put here the mesh of the airplane, the same object assigned to this script
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(0,m_speed*Time.deltaTime,0);
}
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(0,-m_speed*Time.deltaTime,0);
}
if (Input.GetKey(KeyCode.W))
{
Vector3 fwd = new Vector3(0,0,1);
m_airplane.AddForce(fwd*20);
}
}
}
some one could help me to solve this mistery, please?
best regards!
matthew
Answer by Kiloblargh · Jul 10, 2013 at 04:37 PM
In the top left of the editor window, press the button that says Global so that it says Local instead. This will fix your axis display.
Then in your code, you created a Vector3 called fwd, but you did not set it to the airplane's transform.forward. So you are telling it to add force in the global Z direction, not the forward direction, and Unity's just doing what you told it.
Answer by Matthew_Crown · Jul 10, 2013 at 05:13 PM
thanks,
I changed the fwd vector3 inthis way:
Vector3 fwd = m_airplane.transform.forward;
but the problem remain the same. could u suggest me the right script in order to make it working, please?
The localrotation now are visible! :)
matt
Answer by Alnos · Jan 07, 2020 at 11:31 AM
Hey guys I have the same problem here so what's the script line I'm missing to use the local z axis instead of the world z axis?
transform.localPosition.z;
might be the line that you're looking for :)
to set localPosition to new vector write:
transform.localPostion = new Vector3(X, Y, Z);
or you can access an axis separately by adding the .x, .y, or .z at the end hope that helps if you haven't already found the answer.