- Home /
Mouse Rotation And Movement Script with Character Controller
I've been working on a game for nearly a week and, being a near-complete noob with lots of programming experience in anything but c#, I'm stuck while making a movement script.
This is my current code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour {
public CharacterController controller;
public Vector3 MovementSpeed;
public float MouseSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
#region BasicMovement
if (Input.GetKeyDown(KeyCode.LeftShift))
{
if (Input.GetKeyDown(KeyCode.W)) MovementSpeed.z += 4;
if (Input.GetKeyDown(KeyCode.A)) MovementSpeed.x -= 4;
if (Input.GetKeyDown(KeyCode.S)) MovementSpeed.z -= 4;
if (Input.GetKeyDown(KeyCode.D)) MovementSpeed.x += 4;
if (Input.GetKeyUp(KeyCode.W)) MovementSpeed.z -= 4;
if (Input.GetKeyUp(KeyCode.A)) MovementSpeed.x += 4;
if (Input.GetKeyUp(KeyCode.S)) MovementSpeed.z += 4;
if (Input.GetKeyUp(KeyCode.D)) MovementSpeed.x -= 4;
}
else
{
if (Input.GetKeyDown(KeyCode.W)) MovementSpeed.z += 2;
if (Input.GetKeyDown(KeyCode.A)) MovementSpeed.x -= 2;
if (Input.GetKeyDown(KeyCode.S)) MovementSpeed.z -= 2;
if (Input.GetKeyDown(KeyCode.D)) MovementSpeed.x += 2;
if (Input.GetKeyUp(KeyCode.W)) MovementSpeed.z -= 2;
if (Input.GetKeyUp(KeyCode.A)) MovementSpeed.x += 2;
if (Input.GetKeyUp(KeyCode.S)) MovementSpeed.z += 2;
if (Input.GetKeyUp(KeyCode.D)) MovementSpeed.x -= 2;
}
controller.SimpleMove(MovementSpeed);
#endregion
#region Rotation
if (Input.GetAxis("Mouse X") > 0) transform.Rotate((Vector3.up) * MouseSpeed);
if (Input.GetAxis("Mouse X") < 0) transform.Rotate((Vector3.up) * -MouseSpeed);
#endregion
}
} As shown in the code, I'm using a character controller instead of a rigidbody and missing an up/down look script.
The code currently uses the old input system, and I'm hoping to update it to the new system once this movement script is complete, as all I have to do is change the input methods.
P.S. I posted this in help room as well and since I posted it there I updated my move script to the new input system but I'm still stuck and could really do with some help with the actual movement.
Input.GetAxis("Horizontal") will give you -1 when you press A or Left arrow keys, 1 when you press D or Right arrow .Input.GetAxis("Vertical") is the same but with W S and Up Down key. You can use that as a move direction then multiply it to some float for speed
public float walkSpeed, runSpeed;
void Update(){
Vector3 moveDirection = new Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection.Normalized();
float speed = (Input.GetKey(KeyCode.LeftShift))? runSpeed: walkSpeed;
controller.Simple$$anonymous$$ove(moveDirection * speed);
For mouse rotate will be a little bit different, try this tutorial https://youtu.be/_QajrabyTJc
Answer by trannel · Jan 02, 2021 at 08:27 PM
I can see why you are stuck, there are some odd things like GetKeyDown
and a movespeed that increases.. (?) you kind of combined movement direction and speed. it should be sepperate.
so rewrote your code. with comments.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movements : MonoBehaviour
{
public CharacterController controller;
public Vector3 MovementDirection;
public int MoveSpeed = 2;
public float MouseSpeed = 2f;
// Update is called once per frame
void Update()
{
#region BasicMovement
//holding leftshift, move speed will be 4.
if (Input.GetKey(KeyCode.LeftShift))
{
MoveSpeed = 4;
//if (Input.GetKeyDown(KeyCode.W)) MovementSpeed.z += 4;
//if (Input.GetKeyDown(KeyCode.A)) MovementSpeed.x -= 4;
//if (Input.GetKeyDown(KeyCode.S)) MovementSpeed.z -= 4;
//if (Input.GetKeyDown(KeyCode.D)) MovementSpeed.x += 4;
//if (Input.GetKeyUp(KeyCode.W)) MovementSpeed.z -= 4;
//if (Input.GetKeyUp(KeyCode.A)) MovementSpeed.x += 4;
//if (Input.GetKeyUp(KeyCode.S)) MovementSpeed.z += 4;
//if (Input.GetKeyUp(KeyCode.D)) MovementSpeed.x -= 4;
}
else
{
//but normaly , the move speed is 2
MoveSpeed = 2;
// if (Input.GetKeyDown(KeyCode.W)) MovementSpeed.z += 2;
// if (Input.GetKeyDown(KeyCode.A)) MovementSpeed.x -= 2;
// if (Input.GetKeyDown(KeyCode.S)) MovementSpeed.z -= 2;
// if (Input.GetKeyDown(KeyCode.D)) MovementSpeed.x += 2;
// if (Input.GetKeyUp(KeyCode.W)) MovementSpeed.z -= 2;
// if (Input.GetKeyUp(KeyCode.A)) MovementSpeed.x += 2;
// if (Input.GetKeyUp(KeyCode.S)) MovementSpeed.z += 2;
// if (Input.GetKeyUp(KeyCode.D)) MovementSpeed.x -= 2;
}
//Axis Horizontal/Vertical are mapped to the arrow keys and wasd.
// this returns 0 when not pressed.
// -1 when arrow left, and +1 when arrow right.
MovementDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
// and you can multiply a Vector3 by a number.
controller.SimpleMove(MovementDirection * MoveSpeed);
#endregion
#region Rotation
//this means that this can be shortened too.
//instead of this:
//if (Input.GetAxis("Mouse X") > 0) transform.Rotate((Vector3.up) * MouseSpeed);
//if (Input.GetAxis("Mouse X") < 0) transform.Rotate((Vector3.up) * -MouseSpeed);
//use
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * MouseSpeed);
#endregion
}
}
Answer by AverageComet250 · Jan 05, 2021 at 03:16 PM
Thanks for the help and I think I get how the code works @trannel. How would I implement up/down rotation as well, since the current code only goes left/right.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make this scene changing? 1 Answer
2D Character Control Help 0 Answers