Hi, I was trying to create a character controller and I had a problem where when I try to walk it’d neither go sonic speed or just not move at all. Yesterday I couldn’t even get it to move but this morning it moves way too fast.
Here’s the code (don’t mind the jumping part it’s bad and ill fix it later):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour
{
private Rigidbody rigidbody;
public float walkSpeed;
public float jumpPower;
void Awake()
{
rigidbody = gameObject.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float walkMovement = 0;
float strafeMovement = 0;
if (Input.GetKey(KeyCode.W))
{
walkMovement = walkSpeed;
}
if (Input.GetKey(KeyCode.S))
{
walkMovement = -walkSpeed;
}
// --------------------------------------------------------------------
if (Input.GetKey(KeyCode.A))
{
strafeMovement = walkSpeed;
}
if (Input.GetKey(KeyCode.D))
{
strafeMovement = -walkSpeed;
}
// --------------------------------------------------------------------
if (Input.GetKeyDown(KeyCode.Space))
{
rigidbody.AddForce(0, jumpPower, 0);
}
// Move character
rigidbody.AddRelativeForce(Vector3.forward * walkMovement);
rigidbody.AddRelativeForce(Vector3.left * strafeMovement);
}
}