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 Claymore · Aug 19, 2014 at 01:11 PM · 2drotation

Script gives wrong rotation.

So,i have 3D Character in 2D Space,all i wanted to do is to make character HEAD fallow to my mouse direction,so all works PERFECT,untill i start the game,so what happens is that the character head instantly turns up (90 degrees). Anyway the head fallows my mouse but head rotation is not not accurate to mouse position,everything is messed up,head rotatates wrong!

     #pragma strict
      
     var mouse_pos : Vector3;
     var target : Transform; //Assign to the object you want to rotate
     var object_pos : Vector3;
     var angle : float;
      
     function LateUpdate ()
     {
     mouse_pos = Input.mousePosition;
     mouse_pos.z = 15; //The distance between the camera and object
     object_pos = Camera.main.WorldToScreenPoint(target.position);
     mouse_pos.x = mouse_pos.x - object_pos.x;
     mouse_pos.y = mouse_pos.y - object_pos.y;
     angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
     }

I hope that you understand me,please help me out!

Comment
Add comment · Show 4
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 Claymore · Aug 19, 2014 at 01:18 PM 0
Share

I THIN$$anonymous$$ problem is with LateUpdate,but without LateUpdate i cannot turn my character Head Bone :( I want to solve this mystery.

avatar image Scribe · Aug 19, 2014 at 02:06 PM 0
Share

Is this top down or side view? Have a look at Quaternion.LookRotation

avatar image Claymore · Aug 19, 2014 at 02:16 PM 0
Share

2D - SideView,right at the moment my head is full of sh*t and i dont even want to look at Quaternion.LookRotation cause i know its not gonna work. Anyway thanks for your help! :D

avatar image matthewbauer · Aug 19, 2014 at 02:18 PM 0
Share

I think the problem is with worldtoscreebpoint. If the camera is looking 90 degrees down then it should work pretty well, but when you have it at an angle the position starts to get off.

1 Reply

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

Answer by Scribe · Aug 19, 2014 at 02:36 PM

i dont even want to look at Quaternion.LookRotation cause i know its not gonna work.

Just because you don't know how it works doesn't mean it won't work, you are in luck because I felt like proving you wrong :P

 private var mouse_pos : Vector3;
 var target : Transform;
 private var dirUp : Vector3;
 private var dirFwd : Vector3;
 private var zDistance : float;
 
 function Start(){
     zDistance = target.position.z-Camera.main.transform.position.z;
 }
 
 function Update(){
     mouse_pos = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, zDistance));
     dirFwd = mouse_pos-target.position;
     dirUp = Vector3(-dirFwd.y, dirFwd.x, 0);
 }
 
 function LateUpdate (){
     target.rotation = Quaternion.LookRotation(Vector3.forward*Mathf.Sign(dirFwd.x), dirUp*Mathf.Sign(dirFwd.x));
 }


Scribe

Comment
Add comment · Show 10 · 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 Claymore · Aug 19, 2014 at 06:06 PM 0
Share

WOW ITS WOR$$anonymous$$ING LI$$anonymous$$E CHAR$$anonymous$$. BUT,but characters head is lagging behind mouse,like when my mouse is at -90 degrees characters head is in just -45 degrees,its still not accurate to mouse,any ideas ? Thanks for your help,bro.

avatar image Claymore · Aug 19, 2014 at 06:09 PM 0
Share

$$anonymous$$eybe its because "idle" animation is playing and there are some tricks with character bones,dunno,gonna take any advice from you :)

avatar image Scribe · Aug 19, 2014 at 07:51 PM 0
Share

Is it always a constant amount of degree difference, like always 45degrees more, or is it correct at some degrees, like if the mouse is directly above the character does it work?

Can you try disabling the animation attached and see if it works, that is quite likely to be your problem, though I don't work with animations much so I may not be able to help!

avatar image Claymore · Aug 19, 2014 at 09:59 PM 0
Share

Yep problem is with animation,when animation is playing the bones are changing rotation and position or something....and are not letting the head fallow mouse,maybe im not right,BUT animation is the problem. Anyway your script works very well,its very useful and maybe i will try my best to edit it and make everything work with animation. When mouse is directly above character,his head is facing down,dunno why. Yeah,thanks for help man,i appreciate it,Thanks! :)

avatar image Claymore · Aug 20, 2014 at 11:25 AM 1
Share

SOLVED. I did parent bone with empty game object,and added script to empty game object (pivot) and it worked! Thanks for co$$anonymous$$g this long way with me man. THAN$$anonymous$$S!

Show more comments

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

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

Related Questions

Multiple Cars not working 1 Answer

2D sprite that always faces the player? 1 Answer

hide object start script 4 Answers

Delete object when two collision or more 1 Answer

Pressing the enter key to chat script writing TextField How can I activate? 0 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