Question by
noface890 · Dec 23, 2020 at 09:53 PM ·
playerplayer movementglitch
How do I fix my player floating/moving on its own when it hits a wall?,How do I fix my player moving on it's own when
So I'm trying to code a game, but I have this really weird glitch where my player randomly starts moving on its own when I hit a wall. Here's my script if it helps:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementScript : MonoBehaviour {
public float movementSpeed;
public float rotationSpeed;
public float rotX;
public float rotY;
public float rotZ;
// Use this for initialization
void Start () {
}
//Update is called once per frame
void FixedUpdate () {
if (Input.GetKey (KeyCode.LeftShift) && Input.GetKey ("w")) {
transform.position += transform.TransformDirection (Vector3.forward) * Time.deltaTime * movementSpeed * 2.5f;
} else if (Input.GetKey ("w") && !Input.GetKey (KeyCode.LeftShift)) {
transform.position += transform.TransformDirection (Vector3.forward) * Time.deltaTime * movementSpeed;
} else if (Input.GetKey ("s")) {
transform.position -= transform.TransformDirection (Vector3.forward) * Time.deltaTime * movementSpeed;
}
if (Input.GetKey ("a") && !Input.GetKey ("d")) {
transform.position += transform.TransformDirection (Vector3.left) * Time.deltaTime * movementSpeed;
} else if (Input.GetKey ("d") && !Input.GetKey ("a")) {
transform.position -= transform.TransformDirection (Vector3.left) * Time.deltaTime * movementSpeed;
}
}
void Update () {
rotX -= Input.GetAxis ("Mouse Y") * Time.deltaTime * rotationSpeed;
rotY += Input.GetAxis ("Mouse X") * Time.deltaTime * rotationSpeed;
if (rotX < -90) {
rotX = -90;
} else if (rotX > 90) {
rotX = 90;
}
transform.rotation = Quaternion.Euler (0, rotY, 0);
}
}
It's a very simple script. I don't have a player model, and I don't have gravity enabled (since it's a Baldi's Basics style game.) Can anyone help me? Thank you a bunch in advance!
,
Comment