Weird Controls

Hi everyone,

My Character Controller is acting weird.
When I look forward it goes forward.
When I look sideways it goes sideways.
When I look Backwards it goes Backwards.
Heres my mouseLook Script(attached to the Camera, Child of my actual player.):

usingUnityEngine;

//VerysimplesmoothmouselookmodifierfortheMainCamerainUnity
//byFrancisR. Griffiths-Keam - www.runningdimensions.com

[AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
publicclassMouseLook : MonoBehaviour
{
Vector2_mouseAbsolute;
Vector2_smoothMouse;

publicVector2clampInDegrees = newVector2(360, 180);
publicboollockCursor;
publicVector2sensitivity = newVector2(2, 2);
publicVector2smoothing = newVector2(3, 3);
publicVector2targetDirection;
publicVector2targetCharacterDirection;

//Assignthisifthere'saparentobjectcontrollingmotion, suchasaCharacterController.
//Yawrotationwillaffectthisobjectinsteadofthecameraifset.
publicGameObjectcharacterBody;

voidStart()
{
//Settargetdirectiontothecamera'sinitialorientation.
targetDirection = transform.localRotation.eulerAngles;

//Settargetdirectionforthecharacterbodytoitsinitalstate.
if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
}

voidUpdate()
{
//Ensurethecursorisalwayslockedwhenset
Screen.lockCursor = lockCursor;

//Allowthescripttoclampbasedonadesiredtargetvalue.
vartargetOrientation = Quaternion.Euler(targetDirection);
vartargetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);

//Getrawmouseinputforacleanerreadingonmoresensitivemice.
varmouseDelta = newVector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

//Scaleinputagainstthesensitivitysettingandmultiplythatagainstthesmoothingvalue.
mouseDelta = Vector2.Scale(mouseDelta, newVector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));

//Interpolatemousemovementovertimetoapplysmoothingdelta.
_smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
_smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);

//Findtheabsolutemousemovementvaluefrompointzero.
_mouseAbsolute += _smoothMouse;

//Clampandapplythelocalxvaluefirst, soasnottobeaffectedbyworldtransforms.
if (clampInDegrees.x < 360)
_mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);

varxRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
transform.localRotation = xRotation;

//Thenclampandapplytheglobalyvalue.
if (clampInDegrees.y < 360)
_mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);

transform.localRotation *= targetOrientation;

//Ifthere'sacharacterbodythatactsasaparenttothecamera
if (characterBody)
{
varyRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
characterBody.transform.localRotation = yRotation;
characterBody.transform.localRotation *= targetCharacterOrientation;
}
else
{
varyRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
transform.localRotation *= yRotation;
}
}
}

And my movement scripts(attached to the player character):

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    public Transform Trans;
    public Rigidbody rb;
    public int moveSpeed;
    public int sprintIncrease;
    public int strafeSpeed;

    // Use this for initialization
    void Start ()
    {
        Trans = gameObject.GetComponent<Transform>();
        rb = gameObject.GetComponent<Rigidbody> ();
    }
   
    // Update is called once per frame
    void Update () {

        if (Input.GetKey (KeyCode.W))
        {
            rb.transform.Translate(transform.forward*moveSpeed*Time.deltaTime);
        }
        if (Input.GetKey (KeyCode.D))
        {
            rb.transform.Translate(transform.right*strafeSpeed*Time.deltaTime);
        }
        if (Input.GetKey (KeyCode.A))
        {
            rb.transform.Translate(-transform.right*strafeSpeed*Time.deltaTime);
        }
        if (Input.GetKey (KeyCode.S))
        {
            rb.transform.Translate(-transform.forward*strafeSpeed*Time.deltaTime);
        }
        if (Input.GetKey (KeyCode.LeftShift))
        {
            rb.transform.Translate(transform.forward*moveSpeed*sprintIncrease*Time.deltaTime);
        }
        if (Input.GetKeyDown (KeyCode.Space))
        {
            rb.AddForce(0,500,0);
        }

    }
}

Hope You can help.

~Glemau

This must mean I’m up far to late. I’m not seeing the problem.

Isn’t this desired behaviour? Or do you want the player to move in a direction they are not facing?

A quick description of what should happen will help.

Edit: pro tip: you are allowed to put spaces in your comments.

1 Like

Yeah, that would be great :slight_smile: It looks like those find the words puzzles

So I mean the player moves sideways from where i look. So he doesn’t move in the direction the camera is looking