- Home /
[Please Help!] How Can I Make The Tip Of My Ship Follow My Cursor?
Hello, I am asking on behalf of my earlier question about this topic, but I need my ship to look at the cursor when I move it. I already have a mouse look attached to the camera and I can clearly see around at things, but I need my ship to follow it. I also want to be able to have the ship follow it when I press the "Up Arrow". Does anyone have any links or something. Please try to provide C# code if any, thanks.
Answer by robertbu · May 17, 2013 at 07:21 PM
It is easiest to get things to follow if the 'tip' is also 'forward' of the object which is also positive 'Z' in Unity. The next issue is that you are dealing with 3D space, so by saying follow the cursor (and I'm assuming you are talking about the mouse cursor), what you really mean is follow some point projected from my cursor position into the world space beyond. To do that you need to convert the cursor position into a 3D world space position. Assuming you are using the main camera, you can do something like this in Update:
void Update() {
Vector3 v3Pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f));
Transform.LookAt(v3Pos);
}
Note the 10.0 represents 10 units in front of the camera.
For some games, you want to look at the object under the cursor instead of a fixed distance into the game. For these types of games, you will need to use Physics.Raycast() to find the 3D intersecton of the ray from the cursor and the objects.
Do you only want the ship to follow if the Up Arrow is pressed down? What happens if it is following and then the Up Arrow is lifted? Does it drift back to some other position or remain in whatever position was last used?
If the up arrow is lifted, then I want the ship to drift a little bit and then slowly come to a stop/standstill. If I press the back arrow, then I want to make the ship move away or opposite from the mouse/cursor. Or, if the ship is moving forward, pull the ship to a halt faster than just lifting the up arrow. Can you still help me? The script when I put it in says that the LookAt control is requiring an object reference. I thought that it was supposed to point at the cursor? What happened and how can I fix it. Please remember, I am a newbie to coding in Unity.
I not going to write all of your functionality for you, but I can get you a bit further now that I understand a bit more about the arrow keys:
public float speed = 5.0f;
private Quaternion qTo;
void Start () {
qTo = transform.rotation;
}
void Update () {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow)) {
Vector3 v3Pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f));
qTo = Quaternion.LookRotation (v3Pos - transform.position);
}
transform.rotation = Quaternion.Lerp(transform.rotation, qTo, Time.deltaTime * speed);
}
Note I've change the code so that it drifts towards the cursor position over time. This avoids the immediate realignment that would have happened with Transform.LookAt().
Thanks. I'll do my best with it. Thanks for your help. Now, what do I attach this to? The Camera or the ship itself?
The above code needs to be added at a C# class. Then you attach it to whatever you want to point at the mouse cursor. From your description, that is the ship. Again, you must have the front of your ship pointing at positive 'Z' with rotation (0,0,0) for this code to work.
Is it Time.deltaTime or is it Time.time? I'm having trouble with Lerp working.
Your answer
Follow this Question
Related Questions
Need Help Flipping my character depending on if the mouse is left or right. 1 Answer
How can I limit the rotation on the Y axis so the player cant spin the camera 360 in an FPS game? 0 Answers
Help!Crash! FatalError"Callback registration failed kMaxCallback" 1 Answer
How to reset character orientation based on direction the camera is facing 1 Answer