- Home /
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!
I THIN$$anonymous$$ problem is with LateUpdate,but without LateUpdate i cannot turn my character Head Bone :( I want to solve this mystery.
Is this top down or side view? Have a look at Quaternion.LookRotation
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
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.
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
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.
$$anonymous$$eybe its because "idle" animation is playing and there are some tricks with character bones,dunno,gonna take any advice from you :)
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!
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! :)
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!
Your answer
Follow this Question
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