- Home /
Question by
FunFreighterGames · Oct 21, 2014 at 05:45 AM ·
rigidbodycharacterrotateorientation
How to get my rigidbody character to orient rotation to a game object
I took this code from a tutorial online in order to rotate my rigid body character to face the mouse. It works great; however I need to get it working with the xbox controller, so I'm trying to get it to point to a game object being controlled by my right joystick instead of the mouse. I don't really comprehend what's going on in this code though, If someone could break down what's happening for me here that would be really helpful as Quaternions are new to me at this point.
//control rotation playerPlane = new Plane(Vector3.up, transform.position); ray = Camera.main.ScreenPointToRay (Input.mousePosition); Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
float hitdist;
if (playerPlane.Raycast(ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
//rotate
if(this.anim.GetCurrentAnimatorStateInfo(0).IsName("attack") == false){
transform.rotation = targetRotation;
}
}
Comment