Question by
sharkalmousawi · Sep 20, 2021 at 06:10 PM ·
c#scripting problemrotate objectflipping
Player is not flipping
i am trying to flip my player but in the tutorial he use 0.01f and when I try that my player gets really small on movement I want to fix this issue Script code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour { [SerializeField] private float speed; [SerializeField] private float jump;
//Animator and Rigidbody References
private Rigidbody2D body;
private Animator anim;
private bool grounded;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
private void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
//Flip player movement left/right
if(horizontalInput > 44f)
transform.localScale = Vector3.one;
else if(horizontalInput < -44f)
transform.localScale = new Vector3(-1, 1, 1);
if(Input.GetKey(KeyCode.Space) && grounded)
Jump();
//Set Animator parameters
anim.SetBool("Run", horizontalInput !=0);
anim.SetBool("Grounded", grounded);
}
private void Jump()
{
body.velocity = new Vector2(body.velocity.x, jump);
anim.SetTrigger("jump");
grounded = false;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Ground")
grounded = true;
}
}
Comment