- Home /
How do i GET the the YRotation of an object?
Hi, im just starting out. As the title says, i want to get the YRotation from the camera.
I think im supposed to use this?
YRot = GameObject.Find("Camera") . what here?
Thanks alot.
-Ali
Answer by PrinceAli · Jun 13, 2010 at 09:59 PM
Here is the complete script Movement1
var moveSpeed = 10.0; var XRot = 0; var fwd;
private var x : float; private var z : float; function Update () { z = Input.GetAxis("Vertical") Time.deltaTime moveSpeed; x = Input.GetAxis("Horizontal") Time.deltaTime moveSpeed;
fwd = Camera.main.transform.forward; fwd.y = 0; XRot = Vector3.Angle(Vector3.forward, fwd);
// rotate the character based on the x value
//transform.Rotate(0, XRot, 0); transform.eulerAngles = new Vector3(0, XRot, 0);
// Move the character forwards or backwards
transform.Translate(x, 0, z);
}
And here is the error: NullReferenceException: Object reference not set to an instance of an object Movement1.Update () (at Assets\MyScripts\Movement1.js:22)
Did i do it wrong?
Answer by qJake · Jun 13, 2010 at 09:10 PM
YRot = GameObject.Find("Camera").transform.eulerAngles.y
will get you the 0-360 degree rotation of the camera (the same units that the Editor uses).
Be careful if it's for anything important and you're not just rotating around Y, reading eulerAngles will (see$$anonymous$$gly) randomly flip 180 degrees
Answer by PrinceAli · Jun 13, 2010 at 09:21 PM
I just noticed that it flips. So, im trying to make a simple 3rd person camera movement script.
var moveSpeed = 10.0; var XRot = 0;
private var x : float; private var z : float; function Update () { z = Input.GetAxis("Vertical") Time.deltaTime moveSpeed; x = Input.GetAxis("Horizontal") Time.deltaTime moveSpeed;
XRot=GameObject.Find("Camera").transform.eulerAngles.y; // rotate the character based on the x value transform.Rotate(0, XRot, 0); // Move the character forwards or backwards transform.Translate(x, 0, z);
}
But the players started to spin like crazy, what should i do? Im combining this script with the "MouseOrbit" script. Thanks