Hello Community,
Firstly I am very new to coding and working on a 2d game, my character doesn’t flip around to face the direction i want to go.
So in my 2d game I want my character to look left when pressing left and look right when pressing right, at the moment my character will flip but only after a jump command which has completely confused me.
[/code]
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
public float speed;
public float jump;
private float move;
private Rigidbody2D rb;
private bool isJumping;
bool facingRight = true;
public Animator animator;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
}
// Update is called once per frame
void Update()
{
move = Input.GetAxisRaw(“Horizontal”);
animator.SetFloat(“Speed”, Mathf.Abs(move));
rb.velocity = new Vector2(move * speed, rb.velocity.y);
if (Input.GetButtonDown(“Jump”) && !isJumping)
{
rb.AddForce(new Vector2(rb.velocity.x, jump));
isJumping = true;
}
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag(“Ground”))
{
isJumping = false;
}
if (move < 0 && facingRight)
{
flip();
}
else if (move > 0 && !facingRight)
{
flip();
}
}
void flip()
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
}[/CODE]