- Home /
I need mouse look and CharacterController.Move to work on the same object
I have a player model attached to a game object that has the following script on it:
var speed = 4.0;
function Start() { CheckRotation(); }
function Update () { CheckRotation(); CheckMovement(); //WASD Keys are default }
function CheckMovement() { var controller : CharacterController = GetComponent(CharacterController);
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
controller.Move(moveDirection * Time.deltaTime);
}
function CheckRotation() { var playerPlane = new Plane(Vector3.up, transform.position); var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist))
{
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 4.0 * Time.deltaTime); //This isn't instant
}
}
The problem with this script is that the player always moves towards the mouse. I need the player to be able to push W and move north, not forward. If anyone has any ideas that would be great.
This game is for a Top Down shooter by the way. hence why W is north ins$$anonymous$$d of forward.
Answer by GesterX · Feb 09, 2011 at 05:20 PM
I am currently working on a top down game. Here is my code to rotate dependant on the mouse but move the player as you describe ("W" always goes north etc). It is adapted from the Evac City tutorial but it's not a direct copy. This will only work if you have a rigidbody attached to your character. You must also have the player tagged as Player and the mainCamera tagged as MainCamera. Finally to make it "feel" better: Turn off Gravity in your Rigidbody Set the rigidbody mass to 6 and drag to 12. Set the moveSpeed to 25000 in the inspector. In Edit>Project Settings>Input set the gravity and sensitivity on the horizontal and vertical axis to 1000.
public var moveSpeed = 100f;
//input variables private var inputRotation : Vector3; private var inputMovement : Vector3;
// calculation variables private var tempVector : Vector3; private var tempVector2 : Vector3;
function Start() { thePlayer = GameObject.FindWithTag ("Player"); theCamera = GameObject.FindWithTag ("MainCamera"); }
function Update () { FindPlayerInput(); ProcessMovement(); Camera Control(); }
function FindPlayerInput() { //vector for movement inputMovement = Vector3( Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") );
// the position of the middle of the screen
tempVector2 = Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
// find the position of the moue
tempVector = Input.mousePosition;
//keeps the game 2D
tempVector.z = tempVector.y;
tempVector.y = 0;
// the direction we want look is from the centre of the screen to where the mouse is
inputRotation = tempVector - tempVector2;
}
function ProcessMovement() { rigidbody.AddForce (inputMovement.normalized moveSpeed Time.deltaTime); transform.rotation = Quaternion.LookRotation(inputRotation); transform.eulerAngles = Vector3(0,transform.eulerAngles.y + 180,0); transform.position = Vector3(transform.position.x,transform.position.y,transform.position.z); }
function CameraControl() { theCamera.transform.position = Vector3(transform.position.x, 30,transform.position.z); theCamera.transform.eulerAngles = Vector3(90,0,0); }
I'll take a look. I got something kind of work to using positions relative to the camera, but there is a lot of sliding in $$anonymous$$e. Thanks for the code! :)
No problem. Be sure to mark the answer correctly if it solves your problem
Your answer