- Home /
 
 
               Question by 
               oliver-jones · Jun 12, 2012 at 05:26 PM · 
                charactermouserotatecontrol  
              
 
              Character - Move In Mouse Direction
Hello,
I'm trying to move my character using the Character Controller/Motor. So far, my character rotates wherever my mouse is, but I'm struggling to get the character to move in the direction/rotation of itself.
Is is the code that allows my character rotate is the mouses direction:
 function Update () {
     
     
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var playerRotateSpeed = 10;
     
     var hitdist = 0.0;
     var targetPoint = ray.GetPoint(hitdist);
 
     var lookPos = targetPoint - transform.position;
     lookPos.y = 0;
     var targetRotation = Quaternion.LookRotation(lookPos);    
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
 
               Now I'm trying to get the 'motor.inputMoveDirection' to move the character is its current direction. I know I could just use transform.Translate, but I really need to use the character controller/motor.
Thanks
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by oliver-jones · Jun 12, 2012 at 08:36 PM
Okay, so I figured it out.
You want to add the platform controller to your player, and change the platform script to this:
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var playerRotateSpeed = 10;
     
     var hitdist = 0.0;
     var targetPoint = ray.GetPoint(hitdist);
 
     var lookPos = targetPoint - transform.position;
     lookPos.y = 0;
     var targetRotation = Quaternion.LookRotation(lookPos);    
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
         
     var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
     
     if (directionVector != Vector3.zero) {
         var directionLength = directionVector.magnitude;
         directionVector = directionVector / directionLength;
         
         directionLength = Mathf.Min(1, directionLength);
         
         directionLength = directionLength * directionLength;
         
         directionVector = directionVector * directionLength;
     }
     
     directionVector = transform.rotation * directionVector; 
     motor.inputMoveDirection = directionVector; 
 
              Thanks! Is there a benefit to using
 var hitdist = 0.0;
 var targetPoint = ray.GetPoint(hitdist);
 
                  over something like ray.origin or ray.direction? 
Your answer