Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 sonic220 · Jun 23, 2012 at 10:07 AM · camerapositionmouserotatefollow

Camera Rotate to mouse position

Hi guys, I need some help to make this code work, I'm making a point and click game ad I want that in some cases the camera act something like an human head (first person view) I want the camera to rotate where the mouse is looking at making it at the center of the screen, so far I've made this code:

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, 1200)) {
         Debug.DrawLine (ray.origin, hit.point);
         camera_.LookAt(hit.point);
     }

The problem with this code is that the camera spins on itself because the mouse is always pointing to a new world position, how can I make the mouse to move smooth to the center?

Thanks for the help

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

2 Replies

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

Answer by whydoidoit · Jun 23, 2012 at 10:16 AM

    var targetRotation : Quaternion;  //Script variable
    var speed = 2.0;

    ...

    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit, 1200)) {
        Debug.DrawLine (ray.origin, hit.point);
         targetRotation = Quaternion.LookRotation(hit.point - camera_.transform.position);

     }


    ...

    function LateUpdate() {
           transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
    }
Comment
Add comment · Show 3 · 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 sonic220 · Jun 23, 2012 at 10:39 AM 0
Share

Thanks man! This really helped me out! :D

avatar image whydoidoit · Jun 23, 2012 at 10:42 AM 0
Share

Very welcome :)

avatar image SuperRaed · Jul 04, 2016 at 11:01 AM 0
Share

@whydoidoit this might be a noob question, but what if the ray doesn't hit anything? wouldn't the whole expression evaluate to false and none of the statement would be executed?

avatar image
0

Answer by makegoalken · Mar 04, 2020 at 08:16 AM

A very meaningful event, I hope everything will go well temple run

Comment
Add comment · Show 1 · 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 unity_9SNg6qB_Z0twEQ · Apr 17, 2020 at 06:09 AM 0
Share

The problem with this code is that the camera spins on itself because the mouse is always pointing to a new world position, how can I make the mouse to move smooth to the center?

I could not solve this problem too, for my project rapidessay, I hope that I will be able to cope with that.

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

9 People are following this question.

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

Related Questions

Camera rotation around player while following. 6 Answers

Scripting Issue With Top Down Mouse Movement 1 Answer

Camera rotate with mouse in TPC 0 Answers

Aircraft wing position 0 Answers

Camera follow rotating ball 2 Answers


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