- Home /
Unity Look at Mouse Position 2D C#
Hey
I'm trying to make a top down 2D tank game where the turret looks at the mouse. I have a simple script that I thought was working but I have noticed that if the mouse is far away from the tank and the mouse is at an angle to the tank it becomes inaccurate. I want the tank turret to look directly at the mouse at any angle.
Script:
var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Quaternion rot = Quaternion.LookRotation (turretFollow.transform.position - mousePosition, Vector3.forward);
Debug.DrawRay(turretFollow.transform.position, mousePosition);
Debug.DrawRay(turretFollow.transform.up, turretFollow.transform.up * 2, Color.red);
turretFollow.transform.rotation = rot;
turretFollow.transform.eulerAngles = new Vector3 (0, 0, turretFollow.transform.eulerAngles.z);
turretFollow.rigidbody2D.angularVelocity = 0;
float step = turretSpeed * Time.deltaTime;
turret.transform.rotation = Quaternion.RotateTowards(turret.transform.rotation, turretFollow.transform.rotation, step);
I have some photos to help explain what I'm talking about. The red line is the current angle its looking at which is wrong. The white line is the angle I want it to look at but its not and I cant figure out why.
The angle is wrong when around a 45 degree angle and when the cursor is far away:
It does work tho when the cursor is in front of the tank:
Hope you can understand my issue and can help! Thx in advance
If anyone has any questions just ask! ~Wakeful
Answer by Taxen0 · Jan 12, 2015 at 08:51 AM
Try the following and see if it works for you, I haven't tried it but it should be ok:
var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Quaternion rot = Quaternion.LookRotation (mousePosition - turretFollow.transform.position, turretFollow.transform.TransformDirection(Vector3.forward));
turretFollow.transform.rotation = new Quaternion(0, 0, rot.z, rot.w);
Answer by Baste · Jan 12, 2015 at 08:53 AM
You are setting the rotation three times in the same algorithm. The two first ones are being overridden by the third. You're probably a bit confused about what you want to do.
The last one you can safely ditch:
turret.transform.rotation = Quaternion.RotateTowards(turret.transform.rotation, turretFollow.transform.rotation, step);
This will have your tank rotate towards looking in the same direction as the turretFollow is looking, which is not what you want.
Then you have these two lines:
turretFollow.transform.rotation = rot;
turretFollow.transform.eulerAngles = new Vector3 (0, 0, turretFollow.transform.eulerAngles.z);
which are trying to do the same thing. The second line will override the first one.
If you want the tank to just look at the thing, use one of those two lines. The one where you set rotation directly is the one you want unless your tank tends to rotate on the other axes.
If you want the tank to rotate over time, the last line is correct, but you want to rotate towards the look rotation, not the rotation of the target:
turret.transform.rotation = Quaternion.RotateTowards(turret.transform.rotation, rot, step);
Sorry just to clarify I do what the turret to rotate over time. I use the turret follow to set the wanted rotation then I make the turret rotate over time towards the turret follow but something is not working and giving me the wrong rotation.
Edit: sorry for the poorly named variables
Answer by skylem · Jan 12, 2015 at 05:52 PM
use LookAt(); heres an example LookAt(transform); // what you need to use,
How i've used this in the past.
if(hudTarget != null) {
if(hudObject.activeSelf == false) {
hudObject.SetActive(true);
}
hudObject.transform.rotation = Quaternion.LookRotation(hudObject.transform.eulerAngles, hudTarget.transform.eulerAngles);
hudObject.transform.LookAt(hudTarget.transform);
}
else if(hudTarget == null) {
if(hudObject.activeSelf == true) {
hudObject.SetActive(false);
}
this code makes n Objects xform(in this case my HUD Arrow) point at another transform.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Trying To Find Mouse Position On Tap 1 Answer
Converting Input.mousePosition to world coordinates 1 Answer