Can't jump after adding jump animation
For some reason I can't jump after adding a jump animation to my player.
my playerMove script includes the code that makes the animation play using: anim.Play ("JUMP00", -1, 1f);
Here is a picture of my animator controller:
I've made sure that the animations work. The Wait00 animation works fine when playing, but after adding anim.Play ("JUMP00", -1, 1f); my player will no longer jump at all.
Here is my playerMove script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
public class PlayerMove2 : MonoBehaviour {
#region UnityProporties
[Header("Basic Movement")]
public float moveForce = 50;
public float airbornForceMultiplyer = 0.2f;
public AnimationCurve speedGain;
public float maxSpeed = 5;
public float stillJumpMaxSpeed = 3;
public float jumpForce = 6;
[Tooltip("The minimum amount of time(ms) befor you can jump again.")]
public float minimumJumpIterval = 100;
[Tooltip("gravity + gravity * <this number>")]
public float gravityMutiplyer = .5f;
[Header("Physics")]
public float groundCheckOffset = .01f;
#endregion
#region PrivateFields
// Components
private Rigidbody _rb;
private SphereCollider _collider;
public Animator anim;
// Fields
private bool _is_grounded = false;
private int _last_jump;
private bool _is_still_jump = false;
#endregion
// Use this for initialization
void Start() {
_rb = GetComponent<Rigidbody>();
_collider = GetComponent<SphereCollider>();
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update() {
_is_grounded = GroundCheck();
Movement();
Jump();
/**
* ## Fast fall
* if player is falling apply extra gravity.
* physically correct gravity while falling
* FEELS fake
*/
if (_rb.velocity.y < 0) {
_rb.AddRelativeForce(Physics.gravity * gravityMutiplyer);
}
}
private bool GroundCheck() {
if ((int)(Time.unscaledTime * 1000) < (_last_jump + minimumJumpIterval))
return false;
Ray ground_check = new Ray(
transform.position + _collider.center,
Vector3.down
);
RaycastHit hit;
var rc_res = Physics.SphereCast(
ground_check,
_collider.radius,
out hit,
_collider.radius + groundCheckOffset
);
if (rc_res) {
if (!hit.collider.CompareTag("Player")) {
_is_still_jump = false;
return true;
}
}
return false;
}
/**
* ## Get players inputs and handle the movement
*/
private void Movement () {
Vector3 input_dir = GetPlayerInput();
// limit the movement force by the maximum player speed
// applied over a curve.
float movent_mult = speedGain.Evaluate(
_rb.velocity.magnitude / (_is_still_jump ? stillJumpMaxSpeed : maxSpeed)
);
var movement_force = new Vector3(
input_dir.x * moveForce,
0,
input_dir.z * moveForce
);
movement_force *= movent_mult;
if (!_is_grounded) movement_force *= airbornForceMultiplyer;
// Apply the force to the Rigidbody
_rb.AddRelativeForce(movement_force, ForceMode.Force);
}
/**
* ## Jump condition
*/
private void Jump () {
if (Input.GetButton("Jump") && _is_grounded ) {
anim.Play ("JUMP00", -1, 1f);
if (_rb.velocity.sqrMagnitude< .1) {
/*
* if player jumps while standing still
* max speed in the air is limited
*/
_is_still_jump = true;
}
// prevents the downwards momentum from previos jumps
// affecting this one
_rb.velocity.Set(_rb.velocity.x, 0, _rb.velocity.z);
_rb.AddRelativeForce(0, jumpForce, 0, ForceMode.Impulse);
// prevents multiple jumps within the frames where the
// margin of error for the ground check may say the player
// is still grounded
_last_jump = (int) (Time.unscaledTime* 1000);
_is_grounded = false;
}
}
private Vector3 GetPlayerInput () {
var input_dir = new Vector3(
Input.GetAxisRaw("Horizontal"),
0,
Input.GetAxisRaw("Vertical")
);
// The input vectors magnitude should never exceed 1
return input_dir.normalized;
}
}
If you need any more information please ask and I will provide. Any help is appriciated.
Your answer
Follow this Question
Related Questions
I can't see or access a state in the Animation Controler 1 Answer
Character rolls forward,but slides back after animation 2 Answers
I am slowly falling down with the jump animation 4 Answers
Model does not play animation 0 Answers
I can't make transition from actual animation to previous one 1 Answer