Hello, I would like to know if it is possible to add a second selection “Walk Check Collider” for the left of my character or to rotate it to the left when it looks to the left? I have surrounded you with the elements in question and underlined in the script that it corresponds to the element! Can you mark me the solution? Thank you
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public ParticleSystem dust;
public float moveSpeed;
public float climbSpeed;
public float jumpForce;
private bool isJumping;
private bool isGrounded;
[HideInInspector]
public bool isClimbing;
bool isSlinding;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask collisionLayers;
__[B]public Transform wallCheckCollider;
public LayerMask wallLayer;
const float wallCheckRadius = 0.2f;
[SerializeField] float slideFactor = 0.2f;[/B]__
public Rigidbody2D rb;
public Animator animator;
public SpriteRenderer spriteRenderer;
public CapsuleCollider2D playerCollider;
private Vector3 velocity = Vector3.zero;
private float horizontalMovement;
private float verticalMovement;
public static PlayerMovement instance;
private void Awake()
{
if (instance != null)
{
Debug.LogWarning("Il y a plus d'une instance de PlayerMovement dans la scène");
return;
}
instance = this;
}
void Update()
{
animator.SetFloat("yVelocity", rb.velocity.y);
CreateDust();
horizontalMovement = Input.GetAxis("Horizontal") * moveSpeed * Time.fixedDeltaTime;
verticalMovement = Input.GetAxis("Vertical") * climbSpeed * Time.fixedDeltaTime;
if (Input.GetButtonDown("Jump") && isGrounded && !isClimbing)
{
isJumping = true;
animator.SetBool("Jump", true);
}
Flip(rb.velocity.x);
float characterVelocity = Mathf.Abs(rb.velocity.x);
animator.SetFloat("Speed", characterVelocity);
animator.SetBool("isClimbing", isClimbing);
__[B] WallCheck();[/B]__
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, collisionLayers);
MovePlayer(horizontalMovement, verticalMovement);
animator.SetBool("Jump", !isGrounded);
}
[B]__ void WallCheck()
{
if (Physics2D.OverlapCircle(wallCheckCollider.position, wallCheckRadius, wallLayer)
&& Mathf.Abs(horizontalMovement) > 1
&& rb.velocity.y < 0
&& !isGrounded)
{
Debug.Log("SLIDE");
Vector2 v = rb.velocity;
v.y = -slideFactor;
rb.velocity = v;
isSlinding = true;
if(Input.GetButtonDown("Jump"))
{
rb.AddForce(new Vector2(0f, jumpForce));
}
}
else
{
isSlinding = false;
Debug.Log("NOT SLINDING");
}
}__[/B]
void MovePlayer(float _horizontalMovement, float _verticalMovement)
{
if (!isClimbing)
{
Vector3 targetVelocity = new Vector2(_horizontalMovement, rb.velocity.y);
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref velocity, .05f);
if (isJumping)
{
rb.AddForce(new Vector2(0f, jumpForce));
isJumping = false;
}
}
else
{
Vector3 targetVelocity = new Vector2(0, _verticalMovement);
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref velocity, .05f);
}
}
void Flip(float _velocity)
{
CreateDust();
if (_velocity > 0.1f)
{
spriteRenderer.flipX = false;
}else if(_velocity < -0.1f)
{
spriteRenderer.flipX = true;
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
animator.SetBool("Jump", !isGrounded);
}
void CreateDust()
{
dust.Play();
}
}