- Home /
Rotation, my script shouldn't work but does.
LOL. I guess the title says it all. I am doing rotations on objects to face towards their intended target. I had borrowed some of this logic and it seemed to work perfectly on my camera tracking, but it is using the same code and I can't figure out why my fromDirection should be based off my UICamera.
UICAMERA, sits at 0,0,0 and does not rotate and is not attached to any ships flying around. In fact, this camera is pretty much hidden except to view the UI Overlay stuff.
THISSCRIPT and the SpaceShip its attached to is at something like -7000,4,0
The SUCCESSFUL CODE IS:
void TurnTowardsTheTargetVector ()
{
//if we are not in position
Vector3 Point = myTarget.transform.position;
// Direction to something = target.position - start.position
Vector3 toDirection = Point - transform.position;
Vector3 fromDirection = GameObject.FindGameObjectWithTag ("UICamera").transform.forward;
Quaternion toRotation = Quaternion.FromToRotation(fromDirection,toDirection);
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, turningInertia * Time.deltaTime);
}
it shouldn't work but does...
In the Above code, which is part of a larger script that is attached to a spaceship. And it flies around automatically. And when the user clicks a "planet" it [the spaceship] aligns to it. My Confusion is because as I imagine in my mind. The fromDirection should be the transform.forward of the spaceship..... Why in the hell is UICamera even involved or even working perfectly? (as far as I can tell). If anything I would have thought the MainCamera ( which is actually following behind the spaceship ) would have been the issue. I would have thought this code BELOW was the solution:
Vector3 fromDirection = this.transform.forward;
Where THIS, is actually just the spaceship gameobject. When I do however use the this.transform.forward code it kind of works, but only halfway. In fact, literally the camera tracks to about halfway.
I have done Debug.Drawrays on (toDirection) and (this.transform.forward) and they are both right on target.
SOLVED - I changed the code just a bit, and It works perfectly
But what am I not understanding ? =(
... and IT ALSO makes sense to me. But, I ask you all what is it that I am not UNDERSTANDING.... my initial post makes no sense to me.. but this does; This is the newcode:
Quaternion toRotation = Quaternion.LookRotation(myTarget.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, toRotation, 0.5f * Time.deltaTime);
Your answer
Follow this Question
Related Questions
Checking for quaternion values to not be NaN 4 Answers
Camera Rotation Question 1 Answer
A little help with this camera script please? 3 Answers
Lerp Third Person Camera Rotation 1 Answer
camera rotation apparent to player 1 Answer