So i have a script that moves my player plays the animations as needed and jumps.
But there are 2 problems.
1. On idle my characters speed is 0 but where i run the game the speed when i dont move the player goes 0 then 10 then 0 then 10 again, and is this loop of increase and decrease of speed, and the animation for slow run is playing in a buggy way.
2. My jump height is controlled by how fust i am running. I have 3 states of my character,
Idle, SlowRun, FastRun,when i SlowRun then it jumps normal but when i FastRun the jump height is higher.
I will paste my entire code bellow, thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermovement : MonoBehaviour
{
private CharacterController controller;
public float playerSpeed = 0;
public float gravity = 5f;
public float jumpSpeed = 5f;
private Vector3 move = Vector3.zero;
private Animator anim;
private float verticalVelocity;
private void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponentInChildren<Animator>();
}
void Update()
{
Move();
}
private void Move()
{
if (controller.isGrounded)
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
move = new Vector3(horizontal, 0, vertical);
move = transform.TransformDirection(move);
}
if (move == Vector3.zero)
{
Idle();
}
else if (move != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
{
SlowRun();
}
else if (move != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
{
FastRun();
}
if (Input.GetKeyDown(KeyCode.Space))
{
move.y = jumpSpeed;
}
move.y -= gravity * Time.deltaTime;
controller.Move(move * Time.deltaTime * playerSpeed);
}
private void Idle()
{
playerSpeed = 0;
anim.SetFloat("Speed", 0, 0.1f, Time.deltaTime);
}
private void SlowRun()
{
playerSpeed = 10f;
anim.SetFloat("Speed", 0.5f, 0.1f, Time.deltaTime);
}
private void FastRun()
{
playerSpeed = 20f;
anim.SetFloat("Speed", 1, 0.1f, Time.deltaTime);
}
}
What I have tried:
I cant figure out why it does this, I have gone online, whatched a lot of videos but i have no idea. I have tried having 2 CharacterController.Move() functions but the it becomes a mess, atleast for my knowledge of c#.
When i say a mess, i mean that then the jump function doesnt work all the time and when i am near a building or steps i get thrown high up like of the map when i press the jump button.
I can not use LayerMask beacuse i need the enviroment to have different layers for other stuff i want to do.
Thank you.