- Home /
How to get character to face camera direction
I am using the MouseOrbitImproved script for camera control and animator controller with the botcontrol script from the Mecanim tutorial. What I need to have is for the character to always move in the direction the camera is facing when applying movement controls or pressing a mouse button. Right now it continues to move in the direction it's currently facing no matter what direction the camera is.
I have. The character will rotate with the camera, but I want the orbital camera/zoom and no character movement until an input is made. No matter how I try to script it I can't get the inputs to have any effect. When I have a bit I'll post up my script.
Show the code you are having trouble with, it's much easier for someone to help that way than a broad general question like that.
var target : GameObject; var followingDistance : float = 25.0;
 private var rotationAngle : float = 0.0;
 private var cameraY : float = 20.0;
  
 var cameraSpeed = 3.0;
 var targetRotationSpeed = 1.5;
 
    
   function LateUpdate () {
        
  
 //$$anonymous$$ake sure that the object is centered in camera and is always on the same distance
 var mouseY = -5.0 * Input.GetAxis ("$$anonymous$$ouse Y");
 cameraY += mouseY;
  
 //v = target - camera
 var targetCameraPosition : Vector3 = -(target.transform.forward - transform.position).normalized * followingDistance + Vector3.up * cameraY;
  
 //Rotate camera on plane xz
 var mouseX = -5.0 * Input.GetAxis ("$$anonymous$$ouse X");
 rotationAngle += mouseX;
  
 var rotationQuaternion1 : Quaternion = Quaternion.AngleAxis(rotationAngle, Vector3.up);
 targetCameraPosition = rotationQuaternion1 * targetCameraPosition;
 targetCameraPosition += target.transform.position;
 targetCameraPosition = Vector3.Slerp(transform.position, targetCameraPosition, Time.deltaTime * cameraSpeed);
 transform.position = targetCameraPosition;
  
 //$$anonymous$$ake camera look at the object
 var rotationQuaternion2 : Quaternion = Quaternion.LookRotation( (target.transform.position - transform.position).normalized );
 transform.rotation = Quaternion.Slerp(transform.rotation, rotationQuaternion2, Time.deltaTime * cameraSpeed);
  
 //
 //Rotate target accordingly
  
 //$$anonymous$$ake sure that the target will rotate only on xz plane
 var xzRotationVector = (target.transform.position - transform.position).normalized;
 xzRotationVector.y = 0.0;
  
 var targetQuaternion : Quaternion = Quaternion.LookRotation( xzRotationVector );
 target.transform.rotation = Quaternion.Slerp(target.transform.rotation, targetQuaternion, Time.deltaTime * targetRotationSpeed);
 }
This is the camera control script that will turn the player to face the same direction as the camera is facing, but I don't want that to happen until a Get$$anonymous$$eyDown is made. I might want it to snap direction ins$$anonymous$$d of using slerp too.
Answer by Lioki · Mar 19, 2013 at 12:50 AM
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down or Horizontal is pressed.
     if(Input.GetMouseButton(0) || Input.GetButton("Vertical")) {
         transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
     } else {
         transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
     }
Fixed it. Added this to my control script, Thanks for everyones help.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                