Question by
levotyhy · Dec 08, 2019 at 11:06 AM ·
2d gamescript.2d-platformer
hi i want to add a slid and wall jump to my script with animation, i am lost ^^
this i my script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour { private Rigidbody2D myRigidbody;
private Animator myAnimator;
[SerializeField]
private float movementSpeed;
private bool attack;
private bool slide;
private bool facingRight;
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
private bool isGrounded;
private bool jump;
private bool jumpAttack;
[SerializeField]
private bool airControl;
[SerializeField]
private float jumpForce;
// Start is called before the first frame update
void Start()
{
facingRight = true;
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}
void Update()
{
HandleInput();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
isGrounded = IsGrounded() ;
HandleMovement(horizontal);
Flip(horizontal);
HandleAttacks();
HandleLayers();
ResetValues();
}
private void HandleMovement(float horizontal)
{
if (myRigidbody.velocity.y < 0 )
{
myAnimator.SetBool("land", true);
}
if (!myAnimator.GetBool("slide") && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack")&& (isGrounded || airControl))
{
myRigidbody.velocity = new Vector2(horizontal* movementSpeed,myRigidbody.velocity.y); //x = -1, y =0;
}
if (isGrounded && jump)
{
isGrounded = false;
myRigidbody.AddForce(new Vector2(0, jumpForce));
myAnimator.SetTrigger("jump");
}
if (slide && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
{
myAnimator.SetBool("slide", true);
}
else if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
{
myAnimator.SetBool("slide",false);
}
myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
}
private void HandleAttacks()
{
if (attack && isGrounded &&!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack") )
{
myAnimator.SetTrigger("attack");
myRigidbody.velocity = Vector2.zero;
}
if (jumpAttack && !isGrounded && !this.myAnimator.GetCurrentAnimatorStateInfo(1).IsName("JumpAttack"))
{
myAnimator.SetBool("jumpAttack", true);
}
if (!jumpAttack && !this.myAnimator.GetCurrentAnimatorStateInfo(1).IsName("JumpAttack"))
{
myAnimator.SetBool("jumpAttack", false);
}
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
attack = true;
jumpAttack = true;
}
if (Input.GetKeyDown(KeyCode.LeftControl))
{
slide = true;
}
}
private void ResetValues()
{
attack = false;
slide = false;
jump = false;
jumpAttack = false;
}
private bool IsGrounded()
{
if (myRigidbody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] collider = Physics2D.OverlapCircleAll(point.position,groundRadius, whatIsGround);
for (int i = 0; i < collider.Length; i++)
{
if (collider[i].gameObject != gameObject)
{
myAnimator.ResetTrigger("jump");
myAnimator.SetBool("land", false);
return true;
}
}
}
}
return false;
}
private void HandleLayers()
{
if (!isGrounded)
{
myAnimator.SetLayerWeight(1, 1);
}
else
{
myAnimator.SetLayerWeight(1, 0);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
2D movement with gravity shifting 0 Answers
how to make knockback and slowmotion when hit by enemy 0 Answers
Help with 2D platformer (score),For my 2D game sometimes coins get added twice to the score?! 0 Answers
how do I make 2 player sprites not interact? 0 Answers
Player is bouncing when colliding with wall object 0 Answers