I am new to Unity and have started playing around with a simple character control. I have a character that looks at the mouse and can be controlled with the w,a,s and d keys. The problem is that the character look and character move controls are competing with each other, whenever I move the character it rotates in the direction of the movement, then back to the mouse look when finished moving. Is it possible to move a CharacterController without it rotating in the direction of that movement?
My guess is I have to do some sort of conversion between character rotation and camera rotation to get my x and y movement direction. The maths has me a little lost here.
LookAtMouse.js
// LookAtMouse will cause an object to rotate toward the cursor, along the y axis.
//
// To use, drop on an object that should always look toward the mouse cursor.
// Change the speed value to alter how quickly the object rotates toward the mouse.
// speed is the rate at which the object will rotate
var speed = 2.0;
function Update () {
// Generate a plane that intersects the transform's position with an upwards normal.
var playerPlane = new Plane(Vector3.up, transform.position);
// Generate a ray from the cursor position
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Determine the point where the cursor ray intersects the plane.
// This will be the point that the object must look towards to be looking at the mouse.
// Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
// then find the point along that ray that meets that distance. This will be the point
// to look at.
var hitdist = 0.0;
// If the ray is parallel to the plane, Raycast will return false.
if (playerPlane.Raycast (ray, hitdist)) {
// Get the point along the ray that hits the calculated distance.
var targetPoint = ray.GetPoint(hitdist);
// Determine the target rotation. This is the rotation if the transform looks at the target point.
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
// Smoothly rotate towards the target point.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
MainCharacterController.js
var speed =7.0;
var gravity = 20.0;
var cam : Transform;
var playerMesh : Renderer;
static var death : boolean;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function Awake(){
cam = Camera.main.transform;
}
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//The following line makes it so that movement direction cannot be greater than 1 (so that diagonal movement is not faster than normal)
moveDirection.Normalize();
moveDirection *= speed;
//If the movement direction variable contains any values (which it only will if you've pressed something), then do the following:
if (!Mathf.Approximately(moveDirection.sqrMagnitude, 0) ){
transform.rotation = Quaternion.identity;
transform.eulerAngles.y = cam.eulerAngles.y;
var pos = moveDirection;
moveDirection = transform.TransformDirection(moveDirection);
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
if (death){
moveDirection = Vector3.zero;
}
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
Any help/hints are appreciated.