- Home /
Transform.Rotate irrespective of local rotation?
Hello,
I am having slight difficulty implementing a simple linear projectile like motion for a moving transform. During the time the transform is in the air I want to slowly adjust its rotation so it will gradually face the ground during the motion rather than flying up and falling backwards. Here is what I have so far:
if (!rwc.isGrounded) {
currentAir += Time.deltaTime;
transform.Rotate (0.5f, 0, 0); //0.5 deg a frame
}
This works well for a jump where the transform rotation doesn't change however if I jump at an angle the transform will rotate respective of its local "down" rotation and not the global down rotation I would like - this gives a curved jump motion.
Is there any way I can adjust the x rotation in this way?
It sounds like you're manually moving the object forwards, something like transform.Translate(0,0,1);
. That won't give "real" jumping motion. It will be more like a swim$$anonymous$$g.
$$anonymous$$ost jumping either uses the gravity code from the charController scripts, or a rigidbody, which runs all the math for you.
Answer by aldonaletto · Jun 17, 2012 at 03:23 PM
You will never match the rotation to the movement using this method. It's better to adjust the object orientation to direction it's moving, like this:
Vector3 lastPos;
void Start(){ lastPos = transform.position; }
void Update(){ Vector3 dir = transform.position - lastPos; if (dir.magnitude > 0.2f){ // update direction/lastPos only after at least 0.2 distance transform.forward = dir.normalized; // point the movement direction lastPos = transform.position; // update lastPos } } This will make the object set its forward direction always to where it's moving. If you want to align other directions, change the transform vector (use transform.up, for instance).
NOTE: If you want to control the object rotation anyway, define the world space in Rotate, like this:
transform.Rotate (90 * Time.deltaTime, 0, 0, Space.World); // 90 degrees per second
and multiply the angle by Time.deltaTime, so the rotation speed will be constant and expressed in degrees per second, no matter which's the framerate.