- Home /
How to run my jump animation in unity3d?
Hello everyone, I have an issue with my jumping animation which I don't know how to run it and makes it work, I know it might be a stupid question, but I'm very beginner with unity and coding in general. I watched some tutorials about running and walking, which I made my character in blender 2.8 and I imported it to unity and everything works just fine. My problem is that I'm stuck with the jumping bit, and I don't know how it should look like in the animator as well as the script. Could anyone help me, please?. I have three animations for jumping, which is: jumping, falling and landing. I put the animation in the animator and I don't know if it is right or not. Here are some pictures to make it clearer. The blend tree contain idle, walk and run animations and here is the code for movement:
public class Player : MonoBehaviour {
[SerializeField] private float WalkSpeed;
[SerializeField] private float RunSpeed;
private float currentSpeed = 0f;
private float speedSmoothVelocity = 0f;
private float speedSmoothTime = 0.1f;
private float rotationSpeed = 0.3f;
private float gravity = 9.8f;
private float JumpSpeed = 50f;
private Vector3 moveDirection = Vector3.zero;
private Transform MaincameraTransform = null;
private CharacterController controller = null;
private Animator animator = null;
private void Start()
{
controller = GetComponent<CharacterController>();
animator = GetComponent<Animator>();
MaincameraTransform = Camera.main.transform;
}
private void Update()
{
Movement();
Jump();
}
private void Movement()
{
Vector2 movementInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector3 forward = MaincameraTransform.forward;
Vector3 right = MaincameraTransform.right;
forward.y = 0;
right.y = 0;
forward.Normalize();
right.Normalize();
Vector3 DesiredMoveDirection = (forward * movementInput.y + right * movementInput.x).normalized;
Vector3 gravityVector = Vector3.zero;
if (!controller.isGrounded)
{
gravityVector.y -= gravity;
}
if (DesiredMoveDirection != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(DesiredMoveDirection), rotationSpeed);
}
float targetspeed = WalkSpeed * movementInput.magnitude;
if (Input.GetKey(KeyCode.LeftShift))
{
targetspeed = RunSpeed * movementInput.magnitude;
}
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetspeed, ref speedSmoothVelocity, speedSmoothTime);
controller.Move(DesiredMoveDirection * currentSpeed * Time.deltaTime);
controller.Move(gravityVector * Time.deltaTime);
animator.SetFloat("MovementSpeed", 0.5f * movementInput.magnitude, speedSmoothTime, Time.deltaTime);
if (Input.GetKey(KeyCode.LeftShift))
{
animator.SetFloat("MovementSpeed", 1f * movementInput.magnitude, speedSmoothTime, Time.deltaTime);
}
}
Answer by thakkarvishal96 · Nov 11, 2019 at 09:06 AM
@Abdul_Bari Kindly check where you put end of the animation? Its continuing walking or what?
Hello, no because it keeps running the 3 jumping animations and I removed it because I think its wrong, do you have any solution, please?