Jaspreet
Jaspreet

Reputation: 167

Messing up Vectors in C# (Unity Engine)

I'm new to programming in C# in Unity and there is some problems when moving in the z-axis. The problem is that I continue to move when I let go of the up button. However, when I move in the x-axis it is fine, as letting go of the button will stop the player. The code is below:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
public Vector3 motion = new Vector3();
private CharacterController controller;

private bool onGround;

private float xRot, yRot;
public static float X_ROTATION = 0;
public static float Y_ROTATION = 0;
private const float lookSpeed = 2.0f;

public void Start() {
    controller = GetComponent<CharacterController>();
}

public void FixedUpdate() {
    if(Screen.lockCursor) {
        Vector3 impulse = new Vector3();
        if(Input.GetKey(KeyCode.W)) impulse.z+=1;
        if(Input.GetKey(KeyCode.A)) impulse.x-=1;
        if(Input.GetKey(KeyCode.S)) impulse.z-=1;
        if(Input.GetKey(KeyCode.D)) impulse.x+=1;

        if(impulse.sqrMagnitude > 0) {
            motion += Quaternion.Euler(new Vector3(0, xRot, 0)) * impulse.normalized * 0.05f;
        }

        if(onGround) {
            if(Input.GetKey(KeyCode.Space)) {
                motion.y += 0.2f;
            }
        }

        motion.y -= 0.015f;
        Vector3 oldMotion = motion;
        Vector3 oldPos = controller.transform.localPosition;
        controller.Move(motion);
        Vector3 newPos = controller.transform.localPosition;
        motion = newPos - oldPos;
        onGround = oldMotion.y < -0.0001f && motion.y >= -0.0001f;
        motion.x *= 0.8f; 
        motion.y *= 0.8f;
    }
}

public void Update() {
    if(Screen.lockCursor && Input.GetKeyDown(KeyCode.Escape)) {
        Screen.lockCursor = false;  
    }

    if(!Screen.lockCursor && Input.GetMouseButtonDown(0)) {
        Screen.lockCursor = true;   
    }

    if(Screen.lockCursor) {
        xRot += Input.GetAxis("Mouse X") * lookSpeed;
        yRot -= Input.GetAxis("Mouse Y") * lookSpeed;

        if(yRot < -90) yRot = -90;
        if(yRot > 90) yRot = 90;
        if(xRot < -180) xRot += 360;
        if(xRot >= 180) xRot -= 360;

        controller.transform.localRotation = Quaternion.Euler(new Vector3(yRot, xRot, 0));
    }

    Player.X_ROTATION = xRot;
}

}

Upvotes: 0

Views: 561

Answers (2)

Matt Bond
Matt Bond

Reputation: 1422

As Gkills said, you should fix the "depletion" of motion.y for motion.z at the end.

Additionally, you should probably want to swap

motion += Quaternion.Euler(new Vector3(0, xRot, 0)) * impulse.normalized * 0.05f;

for

motion += transform.rotation * impulse.normalized * 0.05f;

Then zero out the impulse.y component to prevent the player flying.

Finally you might want to use Time.deltaTime in your Update (so mouse look is frame rate independent)

Upvotes: 1

Gkills
Gkills

Reputation: 587

The last few statments in FixedUpdate() where you have coded-

motion.x *= 0.8f;
motion.y *= 0.8f;

should also contain

motion.z *= 0.8f;

and may be u dont need-

motion.y *= 0.8f;

Upvotes: 3

Related Questions