- Home /
Look at script wrong at high speeds
I've tried numbers of look at scripts, but whenever they get to a high speed, they are always a little offset. It's hard to explain, so I made 2 .gifs so you can see.
The enemy ship follows you and turns to face you. when it is hit by a projectile, it stops. Once the linecast coming off the enemy hits the player, it resumes looking at the player.
http://i.imgur.com/hTxAJpG.gif
But if the enemy moves at a high speed, it gets offset. I've noticed this problem allot in unity, but in this game I can't let it slide as easily.
http://i.imgur.com/bjz8kKg.gif
Here are some scripts I have used:
var newRotation = Quaternion.LookRotation(transform.position - target.position, Vector3.back);
newRotation.x = 0.0f;
newRotation.y = 0.0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotatespeed);
(currently using that one ^^^)
var dir = transform.position - target.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
var newRotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotatespeed);
,
rigidbody2D.transform.eulerAngles = new Vector3(0,0,Mathf.Atan2((TARGETTRANSFORM.position.y - transform.position.y), (TARGETTRANSFORM.position.x - transform.position.x))*Mathf.Rad2Deg - 90);
I tried all these scripts and noticed no difference. I have tried using 3D and 2D colliders (currently using 2D) and have found no difference. I tried using the Update, LateUpdate, and FixedUpdate. While fixed update is the smoothest, it doesn't fix the problem.
If there is some way to refresh the looking script, or get it to be more accurate (preferably with an adjustable speed), that would be fantastic.
When it happens and is running, could you hit Pause and then check the scene view?
I am wondering if the collisions are hitting one of the ships out of the plane they are on. This coupled with a perspective camera could give you the look of an offset when it actually is looking right at it.
Sorry for the late reply.
It doesn't seem to be looking at it from a perceptive either.
Do you think it's possible to cap how fast the enemy rotates? That way if I came up with a number for when it messes up, it could be prevented.
You shouldn't ever be setting any part of a quaternion to any value unless you know what you are doing. A Quaternion
is made up of x, y, z, and w. None of them have anything to do with the dimensions of space.
Have you tried just setting the forward vector of the ship?
transform.forward = (transform.position - target.position).normalized;
To the question about cap, absolutely - look at $$anonymous$$athf.Clamp. Otherwise, I'm useless with rotation questions.
Your answer
Follow this Question
Related Questions
How not to make a RigidBody not go into ground when tilted foward? 1 Answer
How to make a RigidBody not go into ground when tilted foward? 2 Answers
Character still walking when no key is already pressed 1 Answer
Update speed and physics makes my rigidbody jiggle 2 Answers
move player to its rotating direction? 2 Answers