Scripting Jump with Animation Failure

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));

    }
}
1 Like

TransformVector doesn’t apply any sort of movement to the transform, it’s more of a math function. Follow some Rigidbody controller tutorials on YouTube to learn more about how to move with proper physics.

1 Like

I been watching them and jump works for a few minutes then stops completely. I tried rewriting this recently as follows. Again it only worked for a few minutes and then stopped.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [SerializeField]
    public enum State { loco, jump };
    [SerializeField]
    public State state = State.loco;

    [SerializeField]
    [Range(1, 10)] public float moveSpeed;
    [SerializeField]
    [Range(0, 10)] public float speedMultiplier = 5;
    [SerializeField]
    [Range(0, 10)] public float rotSpeed = 2;

    [SerializeField]
    public bool isGrounded;
    [SerializeField]
    public bool isRunning;

    [SerializeField]
    public Vector3 jump;
    [SerializeField]
    [Range(0, 10)] public float jumpForce = 0.01f;

    private Animator anim;
    private Rigidbody rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        anim = GetComponent<Animator>();
        jump = new Vector3(0.0f, 2.0f, 0.0f);
    }
    void OnCollisionStay()
    {
        isGrounded = true;
    }
    // Update is called once per frame
    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);

        //Uses reverse/mirrored  movement

        if (state == State.loco)
        {
            float x = Input.GetAxisRaw("Vertical") < 0 ? -input.x : input.x;
            rb.angularVelocity = rotSpeed * x * Vector3.up;
        }

        //Jump is still not working and as shown there is no attempted call of animator

        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            rb.AddForce(jump * jumpForce, ForceMode.Impulse);
            isGrounded = false;
        }
    }

    void FixedUpdate()
    {
        //Run using 'r' to toggle run on or off.

        if (Input.GetButtonDown("Run"))
        {
            isRunning = !isRunning;
        }
        if (isRunning)
        {
            moveSpeed = 1.5f;
        }
        else
        {
            moveSpeed = 0.5f;
            MovePlayer();
        }
    }
    //Applies Movement
    void MovePlayer()
    {
        Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        transform.TransformVector(new Vector3(0, 0, input.z * moveSpeed * speedMultiplier));

    }
}

I need help figuring out why it stops working randomly. Also when it “did work” jumpforce stetting didn’t do anything, it jumped too high. Everything else is working (walk and run transitions as intended). Its when I add something to the script it goes to crap.

1 Like

OnCollisionStay isn’t being called because you don’t have the correct signature, so isGrounded is never true, that’s why you can’t jump: Unity - Scripting API: MonoBehaviour.OnCollisionStay(Collision)

Your MovePlayer function also doesn’t actually do anything, as I pointed out in my original response.

1 Like

Yeah I completely deleted this mess and trying to adapt this script to use Animator and get the Animations.

[code=CSharp
{
private Rigidbody rb;

[Header(“Player Movement Inputs”)]

private Vector3 PlayerMovementInput;
private Vector2 PlayerMouseInput;
private float xRot;

[SerializeField] private Transform feetTransform;
[SerializeField] private LayerMask FloorMask;
[SerializeField] private Rigidbody PlayerBody;

[Space]

[SerializeField] private KeyCode ShiftKey = KeyCode.LeftShift;
[SerializeField] private float Speed;
[SerializeField] private float Sensitivity;
[SerializeField] private float JumpForce;

private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
rb = GetComponent();
}
private void Update()

{
//Netowrk
if (isLocalPlayer)
{
return;
}
//Player Inputs
PlayerMovementInput = new Vector3(Input.GetAxis(“Horizontal”), 0f, Input.GetAxis(“Vertical”));
PlayerMouseInput = new Vector2(Input.GetAxis(“Mouse X”), Input.GetAxis(“Mouse Y”));

Running();
MovePlayer();
MovePlayeRotation();

//Shooting bullet

}
private void Running()
//Player Running
{
if (Input.GetKeyDown(ShiftKey))

{
Speed *= 2f;
}

else if (Input.GetKeyUp(ShiftKey))

{
Speed /= 2f;
}
}

private void MovePlayer()

{
//Player Movement
Vector3 MoveVector = transform.TransformDirection(PlayerMovementInput) * Speed;
PlayerBody.velocity = new Vector3(MoveVector.x, PlayerBody.velocity.y, MoveVector.z);

if (Input.GetKeyDown(KeyCode.Space))

{
if(Physics.CheckSphere(feetTransform.position, 0.1f, FloorMask))
{
PlayerBody.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
}

}
}

private void MovePlayeRotation()

{
xRot -= PlayerMouseInput.y * Sensitivity;
transform.Rotate(0f, PlayerMouseInput.x * Sensitivity, 0f);

}

}

/code]

The only part here that is bugging me is not having the movement effected by the mouse when I want to select something on a menu but I don’t know how to add to this or adapt the part with holding the mouse button down using the scroll button and allow zoom which scroll wheel to for the camera to a target transform sin a spawned player prefab for the mouse scripting using Main Camera. I don’t want the camera attached because if the player dies the cam would be destroyed till such as the player re-spawns.[/code]

2 Likes