I'll start this off by saying I am very new to Unity and C#, and I'm making my first solo project after following a tutorial series for Unity. I'm starting to make a first person shooter game, I have camera movement down, and I just wrote some code to handle player movement and acceleration. Initially when moving around a flat plane it works fine, but as soon as but as soon as my player object (PlayerColliderParent) bumps into a cube i have placed into the scene, it starts moving on its own at a constant speed and direction, depending on how it bumped into the cube. There aren't any other scripts that move PlayerColliderParent other than PlayerColliderParentScript.cs.
Here's a video of the problem occuring why is it moving???
Here is PlayerColliderParentScript.cs (attached to PlayerColliderParent)
using System.Collections.Generic;
using UnityEngine;
public class PlayerColliderParentScript : MonoBehaviour
{
public Transform cameraRotation;
public Vector3 directionalInput;
public float accelerationFactor = 0.1f;
public float maxSpeed = 6f;
public Vector3 directionalSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//rotate horizontally with camera
transform.localEulerAngles = new Vector3(0, cameraRotation.localEulerAngles.y, 0);
}
void FixedUpdate()
{
//accelerate while movement key is held
//z axis
if (Input.GetKey(KeyCode.W))
{
directionalInput.z = 1;
if (directionalSpeed.z < maxSpeed)
{
directionalSpeed.z += accelerationFactor;
}
}
else if (Input.GetKey(KeyCode.S))
{
directionalInput.z = -1;
if (directionalSpeed.z < maxSpeed)
{
directionalSpeed.z += accelerationFactor;
}
}
else if (directionalSpeed.z > 0)
{
directionalInput.z = 0;
directionalSpeed.z -= accelerationFactor;
}
else if (directionalSpeed.z < 0)
{
directionalInput.z = 0;
directionalSpeed.z += accelerationFactor;
}
else
{
directionalInput.z = 0;
}
//x axis
if (Input.GetKey(KeyCode.D))
{
directionalInput.x = 1;
if (directionalSpeed.x < maxSpeed)
{
directionalSpeed.x += accelerationFactor;
}
}
else if (Input.GetKey(KeyCode.A))
{
directionalInput.x = -1;
if (directionalSpeed.x < maxSpeed)
{
directionalSpeed.x += accelerationFactor;
}
}
else if (directionalSpeed.x > 0)
{
directionalInput.x = 0;
directionalSpeed.x -= accelerationFactor;
}
else if (directionalSpeed.x < 0)
{
directionalInput.x = 0;
directionalSpeed.x += accelerationFactor;
}
else
{
directionalInput.x = 0;
}
//move the player according to directional speed
Vector3 direction = directionalInput.normalized * Time.deltaTime;
float velocityX = direction.x * directionalSpeed.x;
float velocityY = direction.y * directionalSpeed.y;
float velocityZ = direction.z * directionalSpeed.z;
Vector3 velocity = new Vector3(velocityX, velocityY, velocityZ);
transform.Translate(velocity);
}
}
PlayerColliderParent
have a Rigidbody? – derHugoPlayerColliderParent
does have a rigidbody, what is it's gravity and friction settings? – DekuDesu