flying and using Z rotation (repetitive, but I didnt find an answer for this)
Hello Unity fans and thank you for spending some of your time.
My question is regarding the rotation in the Z axis. I have a space shooter in which the ships follow each other. The IA does it by using this code:
//CurrentTarget is the gameObject that they are currently following;
//AngularSpeed is a multiplier of the rotation. Think of it as the ship maneuvrability;
gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, Quaternion.LookRotation(CurrentTargetTransform.position - transform.position), AngularSpeed * Time.deltaTime);
gameObject.transform.position = gameObject.transform.position + gameObject.transform.forward * CurrentSpeed * Time.deltaTime;
So basically they are like homing missiles.
Then the player controlled ship uses the mouse and it basically flies using this code:
transform.position = transform.position + transform.forward * Time.deltaTime * CurrentSpeed;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation (ray.direction), AngularSpeed * Time.deltaTime);
Explained in a simple way, the ship will rotate and follow the cursor (refer to this for more details: http://answers.unity3d.com/questions/718993/how-to-rotate-game-object-with-mouse-not-the-typic.html )
Now I want to add some Z rotation to that using a slerp. The ship will smoothly rotate its Z axis towards the pointer or towards its CurrentTarget. The faster that the ship is rotating on the Y axis, the faster it will rotate in its Z axis. Maybe I'm not being clear with my words as I dont know how to phrase this so well. Basically I want this: https://www.youtube.com/watch?v=wVe31hEVlJI
The Z rotation doesnt controls where the ship goes, it just add some dynamism and make things look better.
Answer by toromano · Nov 27, 2015 at 11:36 AM
I guess you need something like this:
float LeanSpeed = .05f;
transform.position = transform.position + transform.forward * Time.deltaTime * CurrentSpeed;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Quaternion Direction = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(ray.direction), AngularSpeed * Time.deltaTime);
//move direction to local
Vector3 localDirection = transform.InverseTransformVector(ray.direction);
Quaternion Leaning = Quaternion.AngleAxis(-localDirection.x * LeanSpeed, transform.forward); // use x component as an angle input
transform.rotation = Direction * Leaning;
I hope it helps. Needs some fine tuning though with the LeanSpeed multiplier
Answer by OtreX · Nov 29, 2015 at 06:41 AM
@toromano Thanks for your time. It kind of works. the problem I get is... lets say that I'm attempting an U-turn, it will only go 90 degrees (more or less), then it will stop and the ship will begin to point up. It is worth of mentioning that I have Children gameobjects with colliders, two of them with rigidbodies. I deleted the gameObjects that contain all the colliders and rigidbodies from the ship that I use for testing and i got the same result. I'm playing around with it, if I get something I'll post it here. Thank you for your time :)