- Home /
Look at mouse
Hello, I am looking for a script that makes the game object look at the mouse. I know there are many at Unity Answers, but my case is a bit different. The object I want to rotate is a child of another object, which is a rigidbody tank. This tank interacts with the terrain and objects, meaning it rotates, responds to gravity, has collisions, etc. Now the problem is that I want to rotate the turret on top of the tank, and with all the scripts I have tested, the turret ignores the rotation of the tank. Basicly, I want a script that only rotates the turret in the Y axis, but not change at all the x and z axis. Thank you!
Answer by doomprodigy · Jan 16, 2012 at 02:44 AM
using UnityEngine; using System.Collections;
public class Look : MonoBehaviour {
private Vector3 inputRotation;
private Vector3 mousePlacement;
private Vector3 screenCentre;
void Update () {
FindCrap();
transform.rotation = Quaternion.LookRotation(inputRotation);
}
void FindCrap () {
screenCentre = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
mousePlacement = Input.mousePosition;
mousePlacement.z = mousePlacement.y;
mousePlacement.y = 0;
inputRotation = mousePlacement - screenCentre;
}
}
That should give you what you need, incorporate it into your script attached to the to of the tank.
I'm having a problem with your script. At the third line, it says "$$anonymous$$ identifier: inputRotation".
Ah sorry, I forgot to add some of the script. Answer updated. :)
sorry but, there are still a few issues. In the 1st, 2nd, 3rd, 5th and 10th lines (I counted the empty lines as well), it says the same thing: "A namespace can only contain types and namespace declarations". Btw, this is C Sharp right? sorry if I sound very noobish, I'm very new to coding.
this is for incorporating into your script not your whole script. Its missing the monobehavior parts. Here Ill edit it so its a full script so you get the jist. This works 100% no flaws. I wrote the parts to incorporate into one of your scripts not a whole script.
So you want it to rotate when the tank body rotates? But you also want free rotation of the top using the mouse?. Im sorry but your awfully confusing.