- Home /
,Why Character Is not Flipping ? There is some problem i am not able to see
using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; using UnityEngine;
public class Player : MonoBehaviour { private Rigidbody2D _rigid; [SerializeField] private float _jumpforce = 5.0f; // variable for jumpforce [SerializeField] private bool _grounded = false; [SerializeField] private LayerMask _groundLayer; private bool resetjumpneeded = false; [SerializeField] private float _speed =5.0f; private Playeranimation _playeranim; private SpriteRenderer _playersprite;
void Start()
{
_rigid = GetComponent<Rigidbody2D>(); // handle assign of rigidbody
_playeranim = GetComponent<Playeranimation>(); //handle assigned to _anim
_playersprite = GetComponentInChildren<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
Movement();
checkgrounded();
}
void Movement()
{
float move = Input.GetAxisRaw("Horizontal"); //horizontal input for left /right movement
if (move > 0 )
{
Flip(true);
}
else if (move < 0 )
{
Flip(false);
}
if (Input.GetKeyDown(KeyCode.Space) && _grounded == true) //if space key && grounded == true
{
//jump
_rigid.velocity = new Vector2(_rigid.velocity.x, _jumpforce);
_grounded = false;
resetjumpneeded = true;
StartCoroutine(resetjumpneededroutine());
}
_rigid.velocity = new Vector2(move* _speed, _rigid.velocity.y); //current velocity = new velocity (current x ,jumpforce)
_playeranim.Move(move);
}
void checkgrounded()
{
RaycastHit2D hitinfo = Physics2D.Raycast(transform.position, Vector2.down, 0.6f, 1 << 8); //2dray cast to the ground
Debug.DrawRay(transform.position, Vector2.down * 0.6f, Color.green); //this is to show the raycast in game
if (hitinfo.collider != null)
{
Debug.Log("Hit: " + hitinfo.collider.name);
if (resetjumpneeded == false)
_grounded = true; //grounded = true
}
}
void Flip(bool faceright)
{
if (faceright == true)
{
_playersprite.flipX = false;
}
else if(faceright == false)
{
_playersprite.flipX = true;
}
}
IEnumerator resetjumpneededroutine()
{
yield return new WaitForSeconds(0.1f);
resetjumpneeded = false;
}
}
,using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; using UnityEngine;
public class Player : MonoBehaviour { private Rigidbody2D _rigid; [SerializeField] private float _jumpforce = 5.0f; // variable for jumpforce [SerializeField] private bool _grounded = false; [SerializeField] private LayerMask _groundLayer; private bool resetjumpneeded = false; [SerializeField] private float _speed =5.0f; private Playeranimation _playeranim; private SpriteRenderer _playersprite;
void Start()
{
_rigid = GetComponent<Rigidbody2D>(); // handle assign of rigidbody
_playeranim = GetComponent<Playeranimation>(); //handle assigned to _anim
_playersprite = GetComponentInChildren<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
Movement();
checkgrounded();
}
void Movement()
{
float move = Input.GetAxisRaw("Horizontal"); //horizontal input for left /right movement
if (move > 0 )
{
Flip(true);
}
else if (move < 0 )
{
Flip(false);
}
if (Input.GetKeyDown(KeyCode.Space) && _grounded == true) //if space key && grounded == true
{
//jump
_rigid.velocity = new Vector2(_rigid.velocity.x, _jumpforce);
_grounded = false;
resetjumpneeded = true;
StartCoroutine(resetjumpneededroutine());
}
_rigid.velocity = new Vector2(move* _speed, _rigid.velocity.y); //current velocity = new velocity (current x ,jumpforce)
_playeranim.Move(move);
}
void checkgrounded()
{
RaycastHit2D hitinfo = Physics2D.Raycast(transform.position, Vector2.down, 0.6f, 1 << 8); //2dray cast to the ground
Debug.DrawRay(transform.position, Vector2.down * 0.6f, Color.green); //this is to show the raycast in game
if (hitinfo.collider != null)
{
Debug.Log("Hit: " + hitinfo.collider.name);
if (resetjumpneeded == false)
_grounded = true; //grounded = true
}
}
void Flip(bool faceright)
{
if (faceright == true)
{
_playersprite.flipX = false;
}
else if(faceright == false)
{
_playersprite.flipX = true;
}
}
IEnumerator resetjumpneededroutine()
{
yield return new WaitForSeconds(0.1f);
resetjumpneeded = false;
}
}