Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Whutchison · May 17, 2013 at 12:35 PM · c#cameracharactermouse look

[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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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?

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Whutchison · May 17, 2013 at 07:37 PM 0
Share

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.

avatar image robertbu · May 17, 2013 at 08:18 PM 1
Share

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().

avatar image Whutchison · May 17, 2013 at 08:42 PM 0
Share

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?

avatar image robertbu · May 17, 2013 at 11:05 PM 0
Share

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.

avatar image TheProfessor · Jun 05, 2015 at 06:57 PM 0
Share

Is it Time.deltaTime or is it Time.time? I'm having trouble with Lerp working.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Allow both turning of the mouse and turning of the player to keep the camera pointing towards the player 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges