I have tried several ways to add jump and then add its animation to the following script but nothing works. I have removed it now and need to know how I am going to get this actually working with a compatible script! I also cannot get any raycast detection just so the player can detect and walk up and down steps.
Its a 3D rigidbody setup and I don’t like trying to use Character Controllers because they Slide on every angle regardless what I have tried to code to prevent that, such as on a hill as apposed to say a ramp or an actual intended to be slick surface. Then there is the getting stuck on edges issue. Sure I can add a zero friction to the collider on a generic 3D character, but that then has the same problem with everyting else.
In any case this is my basic player control script and it would be very helpful if someone can provide me the correct script that will work with this after the have tested it, and I have loomed at the samples in the Coding API directory and nothing is doing what is intended.
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public enum State { loco, jump };
public State state = State.loco;
private Animator anim;
private Rigidbody rb;
public Vector3 Move;
[Range(1, 10)] public float moveSpeed;
[Range(1, 10)] public float speedMultiplier = 5;
[Range(1, 10)] public float rotSpeed = 2;
public bool isRunning;
// Start is called before the first frame update
public void Start()
{
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
public void Update()
{
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
anim.SetFloat("InputX", input.x * moveSpeed);
anim.SetFloat("InputY", input.z * moveSpeed);
//Apply motion
if (state == State.loco)
{
float x = Input.GetAxisRaw("Vertical") < 0 ? -input.x : input.x;
rb.angularVelocity = Vector3.up * x * rotSpeed;
}
//Run usinf r to toggle run on or off.
if (Input.GetButtonDown("Run"))
{
isRunning = !isRunning;
}
if (isRunning)
{
moveSpeed = 1.5f;
}
else
{
moveSpeed = 0.5f;
MovePlayer();
}
}
private void MovePlayer()
{
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.TransformVector(new Vector3(0, 0, input.z * moveSpeed * speedMultiplier));
}
}