- Home /
Question by
DiamondMC102 · Aug 09, 2017 at 10:12 AM ·
animation2d gameanimator2d-platformer2d-physics
Play and Stop Animation
I’m currently developing a game, but I fell in what seems to be an endless hole in which I can not escape. Basically, I want to my player to play an animation when he jumps and then stop when he touches the ground I have already establish ground collision check. Now I’m not asking you to code it for me, but to help with what I’m working with or lead me to actual sources, Here are my codes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Indingo_Jump_01 : MonoBehaviour {
private bool isJumping;
private void Start()
{
isJumping = false;
}
[Range(1, 10)]
public float jumpVelocity;
private void Update()
{
if (Input.GetButtonDown("Jump") && !isJumping)
{
GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;
isJumping = true;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
isJumping = false;
}
}
}
___________________________________________________________________________________________________________
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Indingo_Jump_02 : MonoBehaviour {
public float fallMultiplier = 2.5f;
public float lowJumpMultiplier = 2f;
Rigidbody2D rb;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
}
}
}
Comment
Best Answer
Answer by ShadyProductions · Aug 09, 2017 at 10:19 AM
Set up states in your animation thingy, you can have an idle state, jumping state etc. And you can switch states in code by accessing the animator, I believe there are tutorials on it on the Unity learn page.