Start with a tutorial for the kind of movement you want. There’s only a few basic choices / combinations, just choose the one you’re looking for.
If that doesn’t help you, absolutely NOTHING typed in this little text box will either.
Two steps to tutorials and / or example code:
do them perfectly, to the letter (zero typos, including punctuation and capitalization)
stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
I’ve copy and pasted this from some old stuff I still use as it just works simply .
Calling this function as CharController(controllerXvalue, controllerYvalue, button1, button2, button3)
attached to your player mesh with a RIGIDBODY on it, and use the new Input System to get keyboard/controller values as they’re just easier to deal with.
As Kurt says, you’ll have to follow tutorials if you want to understand the basics.
public float playerSpeed = 1;
public float currentSpeed = 1;
public float playerWalkSpeed = 5f; // normal speed
public float playerRunSpeed = 10f; // speed character runs
public int rotatePlayerValue;
[Range(0, 30)] public float turningRate = 8; // speed character turns to face other position
public Rigidbody playerRigidbody;
private Quaternion _targetRotation = Quaternion.identity;
void CharController(float x, float y, bool interactButton, bool throwButton, bool runButton)
{
if (currentSpeed > playerWalkSpeed)
{
playerSpeed -= 10 * Time.deltaTime;
}
else
{
playerSpeed = playerWalkSpeed;
}
interactPressed = interactButton;
functionPressed = throwButton;
currentSpeed = playerSpeed;
float angle;
angle = Mathf.Atan2(x, y) * Mathf.Rad2Deg; //work out angle using atan 2
if (x != 0f || y != 0f)
{
_targetRotation = Quaternion.Euler(0, angle + rotatePlayerValue, 0);
transform.rotation = Quaternion.RotateTowards(transform.rotation, _targetRotation, (turningRate * 100) * Time.deltaTime);
}
playerRigidbody.velocity = new Vector3(x * playerSpeed, playerRigidbody.velocity.y, y * playerSpeed);
playerRigidbody.AddForce(transform.forward);
}