Question by
Diamondville · Jul 14, 2017 at 12:45 PM ·
bugjumpinglooping
pls help my character have infinite jumping
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public delegate void DeadEventHandler();
public class Player : Character {
private static Player instance;
public event DeadEventHandler Dead;
public static Player Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<Player>();
}
return instance;
}
}
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
[SerializeField]
private bool airControl;
[SerializeField]
private float jumpForce;
private bool immortal = false;
private SpriteRenderer spriteRenderer;
[SerializeField]
private float immortalTime;
private float direction;
private bool move;
public Rigidbody2D myRigidbody { get; set; }
public bool Jump { get; set; }
public bool OnGround { get; set; }
public override bool IsDead
{
get
{
if (health <= 0)
{
OnDead();
{
SceneManager.LoadScene("GameOverScene");
}
}
return health <= 0;
}
}
private Vector2 startPos;
// Use this for initialization
public override void Start()
{
base.Start();
startPos = transform.position;
spriteRenderer = GetComponent<SpriteRenderer>();
myRigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
if (!TakingDamage && !IsDead)
{
if (transform.position.y <= -14f)
{
Death();
}
HandleInput();
}
}
// Update is called once per frame
void FixedUpdate()
{
if (!TakingDamage)
{
float horizontal = Input.GetAxis("Horizontal");
OnGround = IsGrounded();
if (move)
{
HandleMovement(direction);
Flip(direction);
}
else
{
HandleMovement(horizontal);
Flip(horizontal);
}
HandleLayers();
}
}
public void OnDead()
{
if (Dead != null)
{
Dead();
}
}
private void HandleMovement(float horizontal)
{
if (myRigidbody.velocity.y < 0)
{
MyAnimator.SetBool("land", true);
}
if (!Attack && (OnGround|| airControl))
{
myRigidbody.velocity = new Vector2(horizontal * movementSpeed, myRigidbody.velocity.y);
}
if (Jump && myRigidbody.velocity.y == 0)
{
myRigidbody.AddForce(new Vector2(0, jumpForce));
}
MyAnimator.SetFloat("speed", Mathf.Abs(horizontal));
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.W))
{
MyAnimator.SetTrigger("jump");
Jump = true;
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
MyAnimator.SetTrigger("attack");
}
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
ChangeDirection();
}
}
private bool IsGrounded()
{
if (myRigidbody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
return true;
}
}
}
}
return false;
}
private void HandleLayers()
{
if (!OnGround)
{
MyAnimator.SetLayerWeight(1, 1);
}
else
{
MyAnimator.SetLayerWeight(1, 0);
}
}
private IEnumerator IndicateImmortal()
{
while (immortal)
{
spriteRenderer.enabled = false;
yield return new WaitForSeconds(.1f) ;
spriteRenderer.enabled = true;
yield return new WaitForSeconds(.1f);
}
}
public override IEnumerator TakeDamage()
{
if (immortal)
{
}
health -= 10;
if (!IsDead)
{
MyAnimator.SetTrigger("damage");
immortal = true;
StartCoroutine(IndicateImmortal());
yield return new WaitForSeconds(immortalTime);
immortal = false;
}
else
{
MyAnimator.SetLayerWeight(1, 0);
MyAnimator.SetTrigger("die");
}
}
public void BtnJump()
{
MyAnimator.SetTrigger("jump");
Jump = true;
}
public void BtnAttack()
{
MyAnimator.SetTrigger("attack");
}
public void BtnMove(float direction)
{
this.direction = direction;
this.move = true;
}
public void BtnStopMove()
{
this.direction = 0;
move = false;
}
public void BtnPaused()
{
MyAnimator.SetTrigger ("Paused");
}
public override void Death()
{
myRigidbody.velocity = Vector2.zero;
MyAnimator.SetTrigger("idle");
health = 30;
transform.position = startPos;
if (health <=0)
{
}
}
private void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "Coin")
{
GameManager2.Instance.CollectedCoins++;
Destroy(other.gameObject);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
While (starting jumping or in jump) holding W key makes game object jump high and fall slow. 1 Answer
A tiny sound bug I'm not sure how to solve. 1 Answer
3D Jumping Collision Bug 1 Answer
Is it possible to be on both the App thread and the UI thread? -1 Answers
Terrain editor always switches to paint hole mode,Terrain editor switches to paint holes 0 Answers