Tranform.Forward no longer forward after rotating?
I'm using iTween to move a camera forward/backwards/left/right on a grid. I added the ability to rotate 90 degrees, which is working fine, but once rotated transform.forward doesn't seem to be forward for the camera anymore because now when I press my forward key "Positive on the Vertical Axis" it goes a different direction. Any ideas?
public bool _moving;
void Update ()
{
if (Input.GetAxis ("Vertical") > 0 && !_moving)
{
_moving = true;
iTween.MoveBy (gameObject, iTween.Hash ("amount", transform.forward*10, "easeType", "easeInOutExpo", "oncomplete", "DoneMoving"));
}
if (Input.GetAxis ("Vertical") < 0 && !_moving)
{
_moving = true;
iTween.MoveBy (gameObject, iTween.Hash ("amount", (transform.forward*10) * -1, "easeType", "easeInOutExpo", "oncomplete", "DoneMoving"));
}
if (Input.GetAxis ("Horizontal") > 0 && !_moving)
{
_moving = true;
iTween.MoveBy (gameObject, iTween.Hash ("amount", transform.right*10, "easeType", "easeInOutExpo", "oncomplete", "DoneMoving"));
}
if (Input.GetAxis ("Horizontal") < 0 && !_moving)
{
_moving = true;
iTween.MoveBy (gameObject, iTween.Hash ("amount", (transform.right*10) * -1, "easeType", "easeInOutExpo", "oncomplete", "DoneMoving"));
}
if (Input.GetButton ("RotateRight") && !_moving)
{
_moving = true;
iTween.RotateBy (gameObject, iTween.Hash ("amount", transform.up / 4, "easeType", "easeInOutExpo", "oncomplete", "DoneMoving"));
}
if (Input.GetButton ("RotateLeft") && !_moving)
{
_moving = true;
iTween.RotateBy (gameObject, iTween.Hash ("amount", (transform.up / 4) * -1, "easeType", "easeInOutExpo", "oncomplete", "DoneMoving"));
}
}
void DoneMoving ()
{
_moving = false;
}
What I usually tell others. Debug console is your best friend. Put debug.log inside statements / functions and go from there.
Answer by RenStrike · Nov 30, 2015 at 02:19 AM
Figured it out. When you want the equivalent of "transform.backward" or "transform.left" you need to do the "* -1" on it alone, and not at the end. New working code below.
public bool _moving;
public float _moveSpeed;
public string _moveEaseType;
void Update ()
{
if (Input.GetAxis ("Vertical") > 0 && !_moving)
{
_moving = true;
iTween.MoveTo (gameObject, iTween.Hash ("position", transform.position + transform.forward * 10, "easeType", _moveEaseType, "time", _moveSpeed, "oncomplete", "DoneMoving"));
}
if (Input.GetAxis ("Vertical") < 0 && !_moving)
{
_moving = true;
iTween.MoveTo (gameObject, iTween.Hash ("position", transform.position + (transform.forward * -1) * 10, "easeType", _moveEaseType, "time", _moveSpeed, "oncomplete", "DoneMoving"));
}
if (Input.GetAxis ("Horizontal") > 0 && !_moving)
{
_moving = true;
iTween.MoveTo (gameObject, iTween.Hash ("position", transform.position + transform.right * 10, "easeType", _moveEaseType, "time", _moveSpeed, "oncomplete", "DoneMoving"));
}
if (Input.GetAxis ("Horizontal") < 0 && !_moving)
{
_moving = true;
iTween.MoveTo (gameObject, iTween.Hash ("position", transform.position + (transform.right * -1) * 10, "easeType", _moveEaseType, "time", _moveSpeed, "oncomplete", "DoneMoving"));
}
if (Input.GetButton ("RotateRight") && !_moving)
{
_moving = true;
iTween.RotateBy (gameObject, iTween.Hash ("amount", transform.up / 4, "easeType", _moveEaseType, "time", _moveSpeed, "oncomplete", "DoneMoving"));
}
if (Input.GetButton ("RotateLeft") && !_moving)
{
_moving = true;
iTween.RotateBy (gameObject, iTween.Hash ("amount", (transform.up / 4) * -1, "easeType", _moveEaseType, "time", _moveSpeed, "oncomplete", "DoneMoving"));
}
}
void DoneMoving ()
{
Debug.Log("Forward is " + transform.forward);
_moving = false;
}
Your answer
Follow this Question
Related Questions
How to randomize a single action among many objects PLEASE help! 1 Answer
How do I stop my character feeling like they're on ice using RigidBody2D Velocity 1 Answer
iTween non interpolate PutOnPath method Waypoints 0 Answers
Screen flickers when iTween function gets executed. 0 Answers
Itween Path change speed 0 Answers