Hello lovely Community,
to Time i try me in a first GameJam, to learn more. Now i have a little Problem. Well:
My Player move left by press left mousebutton, move right by press right mousebutton and jump by pressing middle mousebutton. Now i want Animate the Movement, when the Player move. This is my Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public float jumpForce = 10f;
public LayerMask groundLayer;
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
MoveLeft();
}
else if (Input.GetMouseButtonDown(1))
{
MoveRight();
}
else if (Input.GetMouseButtonDown(2))
{
Jump();
}
}
private void FixedUpdate()
{
// Check if the player is on the ground
bool isGrounded = Physics2D.OverlapCircle(transform.position, 0.2f, groundLayer);
}
private void MoveLeft()
{
rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
transform.localScale = new Vector2(-1f, 1f);
GetComponent<Animation>().Play("moveleft");
}
private void MoveRight()
{
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
transform.localScale = new Vector2(1f, 1f);
GetComponent<Animation>().Play("moveright");
}
private void Jump()
{
if (Physics2D.OverlapCircle(transform.position, 0.2f, groundLayer))
{
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}
}
How you can see, i try to start on the Component “Animation” my Animations “moveleft” and “moveright”. But when i test it, it doenst play the animation. Here are the Component:
Anybody a Idea what i do wrong?
Best Regards