- Home /
Rotate Turret where touched (top down view)
I have a top down view of a turret(player) and I want it to aim where the user touches. How do I rotate it, on XZ plane, to face the user's touch?
Ive tried
Quaternion.Slerp(player.transform.rotation, touch, 6 * bananas);
but it has failed me..
Without knowing exactly what failed,I could only suggest to try and freeze the Y rotation of it assu$$anonymous$$g that that's the problem.
var lookPos = touch - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
player.transform.rotation = Quaternion.Slerp(player.transform.rotation, rotation, 6 * bananas);
I did something like this. But it wasn't turning properly.
Edit: touch
is type Vector3
If touch is a screen touch, it will be in screen coordinates and will need to be translated to world coordinates before you can subtract transform.position. You can use Camera.ScreenToWorldPoint().
Answer by Gucci Gagan · Sep 17, 2013 at 11:51 PM
Thank you sooo much robertbu! that fixed it! Heres the code if other people need it. :)
var tapCount = Input.touchCount;
if(tapCount > 0)
{
Vector3 touch = Input.GetTouch(0).position;
Vector3 screen = Camera.main.ScreenToWorldPoint(new Vector3(touch.x, touch.y, Camera.main.nearClipPlane + 5.0f));
var lookPos = screen - player.transform.position;
lookPos.y = 0;
var rot = Quaternion.LookRotation(lookPos); // now get the desired rotation
player.transform.rotation = Quaternion.Slerp(player.transform.rotation, rot, 6 * Time.deltaTime);
}
theere is an error on you script. ( expected. Insert a semicolon at the end.) how can fix that?
Your answer
Follow this Question
Related Questions
Object won't rotate correctly 2 Answers
2D look at 1 Answer
3D nested Turret Prefab Rotation 1 Answer
How to rotate ? 2 Answers
Get my head around Unity Rotations 1 Answer