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 nirharpaz · Dec 02, 2014 at 06:20 PM · lookatcursorlookrotationlooklookattarget

Make player look at pointed target

Hi. I'm working on a 3rd person game and trying to make the player look at the object the cursor points at.

what i try to do is sending a raycast from cursor, hitting the object, then calculating the direction from player to hit.point and rotating there.

 #pragma strict
 private var _mousePos        :Vector3;
 private var _myTransform    :Transform;
 var mouseMovementFactor        :float=1;
 
 function Start () {
         _myTransform = transform;
 
 }
 
 function Update () {
     
     _mousePos = Input.mousePosition;
 //    print(_mousePos);
     var myScreenPos:Vector3 = Camera.main.WorldToScreenPoint(_myTransform.position);
 //    print(myScreenPos);
     var dir2Cursor:Vector3 = _mousePos-myScreenPos;
     //print(dir2Cursor);
     var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit:RaycastHit;
     var direction:Vector3;
     var rotation:Quaternion;
         if(Physics.Raycast (ray,hit,10000) && hit.collider.tag!="Player") 
         {
             direction = (hit.point - _myTransform.position);
             print("hitting "+hit.collider + "   direction from player: "+direction);
         }
     rotation = Quaternion.LookRotation(direction);

 

the part of rotating to look at the direction causes me problems. as for now - the line print("hitting "+hit.collider + " direction from player: "+direction); prints exactly the object i'm touching.

what i tried so far was

 rotation = Quaternion.LookRotation(direction);
     _myTransform.rotation = Quaternion.Slerp(_myTransform.rotation,rotation,Time.deltaTime*mouseMovementFactor);

player rotates rightwards only and would only slow down if i take cursor left.

     _myTransform.rotation.y = Quaternion.Slerp(_myTransform.rotation,rotation,Time.deltaTime*mouseMovementFactor).y;
     _myTransform.rotation.z = Quaternion.Slerp(_myTransform.rotation,rotation,Time.deltaTime*mouseMovementFactor).z;

(x makes it flip on its back) makes it to slerp to a specific rotation and not move from there.

 _myTransform.LookAt(hit.point);

makes it completely go crazy.

 var newDir = Vector3.RotateTowards(_myTransform.forward, direction, mouseMovementFactor*Time.deltaTime, 0.0);
     _myTransform.rotation.y = Quaternion.LookRotation(newDir).y;
     _myTransform.rotation.z = Quaternion.LookRotation(newDir).z;

y - barely moves it z makes it go strange irrelevent directions

please 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jenci1990 · Dec 02, 2014 at 06:38 PM

Try this:

 //This is good.
 direction = (hit.point - _myTransform.position);
 //And add this:
 _myTransform.forward = direction.Normalize();

if wrong way, try the others:

 _myTransform.forward = direction.Normalize() * -1; //backward
 
 _myTransform.right = direction.Normalize(); //*-1 //backward
 _myTransform.up = direction.Normalize(); //*-1 //backward
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 nirharpaz · Dec 02, 2014 at 09:44 PM 0
Share

in both cased i have recieved BCE0022: Cannot convert 'void' to 'UnityEngine.Vector3'." because Normalize(); is a void function

i tried both

  _myTransform.forward = direction.normalized;

 

and

   _myTransform.forward = direction.normalized;
   
   _myTransform.right = direction.normalized;
   _myTransform.up = direction.normalized;

 

the payer messed up and started rotating hysterically to all directions. I believe in need some version with a Time.deltaTime somewhere no?

avatar image jenci1990 · Dec 02, 2014 at 10:01 PM 0
Share

I use Unity 4.6. You should use this: _myTransform.forward = Vector3.Normalize(direction);

avatar image nirharpaz · Dec 03, 2014 at 09:25 AM 0
Share

ok again it goes crazy but i learned new thing. atm i have the Player at the top of the hierarchy. inside there is the player model and the camera.

when i put it on the player model it acts like an pointing arrow (need to sort it to happen over time) but when i put ir on the player.. that moves the camera with him, it all messes up.

i needd it to be on the player

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how to get my look at script to only work on the Y axis? 1 Answer

LookAt doesnt work with cardboard SDK or any camera movement 0 Answers

Look Rotation flipping object rotation after 180 degrees 1 Answer

Movement along X and Z axis... 2 Answers

transform.LookAt is Reverse ! 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