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 /
This question was closed May 03, 2014 at 09:38 PM by AlucardJay for the following reason:

Other

avatar image
0
Question by Arcoss · May 03, 2014 at 09:20 PM · c#cursor

How to make an object point at the cursor

How can I maek an object(a flashlight in my case) point at the cursor all the time

Comment
Add comment · Show 3
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 robertbu · May 03, 2014 at 10:09 PM 0
Share

@alucardj - I did a search to close this as a dup, but none of the links I tried on the first page yielded a viable answer or they involved Raycasting.

avatar image AlucardJay · May 03, 2014 at 10:17 PM 0
Share

I closed this not as a duplicate but as a write-my-code question. There is no detailed information (2D,3D,view perspective), and there is no indication the OP has read or implemented anything to try and solve the problem themselves.

avatar image robertbu · May 03, 2014 at 11:23 PM 0
Share

@alucardj - Yep, your right.

1 Reply

  • Sort: 
avatar image
0

Answer by robertbu · May 03, 2014 at 09:23 PM

A cursor is 2D, so it's world position depends on whatever distance from the camera you want to use when you translate Screen coordinates to World coordinates. Here is a bit of code that will look at the cursor. Values for 'factor' below 1.0 will cause the object look towards the camera. Values greater than 1.0 will cause the object to look away from the camera. Experiment with different values for 'factor' to get the angle of rotation you want.

 #pragma strict
 
 var factor = 0.7;  
  
 function Update() {
     var dist = (transform.position.z - Camera.main.transform.position.z) * factor;
     var pos = Input.mousePosition;
     pos.z = dist;
     pos = Camera.main.ScreenToWorldPoint(pos);
     transform.LookAt(pos);
 }

Note if the camera moves or is not axis aligned, you'll want to change line 6 to:

  var dist = Vector3.Distance(transform.position, Camera.main.transform.position) * factor;

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Left Click Script Problems 1 Answer

error CS8025: Parsing error 1 Answer

C# Track Mouse Cursor Movement Speed 2 Answers

Distribute terrain in zones 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 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