I’m creating a movement system from scratch, using transform.Tranlate
, since I didn’t like using character controller and want constant movement speed.
void Update () {
if(Physics.Raycast(transform.position, -transform.up,1f)) _grounded = true;
else _grounded = false;
if(_grounded )
{
//Jumping
if (Input.GetKeyDown(KeyCode.Space))
{
rigidbody.AddRelativeForce(transform.up * 7, ForceMode.Impulse);
}
}
else
{
//Falling
rigidbody.AddRelativeForce(transform.up * -20, ForceMode.Force);
}
//Movement
if(!Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.W)) transform.Translate(0, 0, 1);
if(!Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.S)) transform.Translate(0, 0, -1);
if(Input.GetKey(KeyCode.D) ) transform.Translate(1, 0, 0);
if(Input.GetKey(KeyCode.A)) transform.Translate(-1, 0, 0);
The problem is that my capsule seems to move on its own, especially after running towards a wall or other element with a collider. It’s even worse since this movement cannot be stopped - sometimes it stops itself after a while.
Capsule has capsule collider and rigidbody attached (Doesn’t use gravity, since the script does it). I found the solution to the stange movement problem by freezing position x an z in rigidbody (y is needed for force jumps). This solves the strange movements but now my capsule doesn’t collide when I’m walking into something like cubes.
I feel like I’m running in circles here trying to fix another bug caused by the bug before, so I’m asking you guys - is there a simple solution to those problems? I’d rather not go back to using the character controller because this way it’s easier for me to add new functions like sprinting or sneaking, and I’m not a unity or C# pro.