Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

need help making a simple ascending enemy

$
0
0
So as the question says, i want to make an enemy that simply ascends after some seconds. I achieve this by using a coroutine. But when it comes to the actual code of making the enemy fly, and follow along beneath the ceiling i'm running into issues. I'm doing a raycast check that if it collides with the ceiling, it moves to the right. The way im making it fly, is by after 2 seconds i set the gravity to -2, so it levitates into the air untill the raycast hits the ceiling, and then it moves right. But i want it to move back to the left, when it's out of the colliding bounds. My code is as follows: public Transform sightStart, sightEnd, flyUpSight; public float speed = 15; public bool moving = false; public bool needsCollision = true; public bool collision = false; public LayerMask whatIsColliding; public Transform groundcheck; public bool grounded = true; public float groundRadius = 0.2f; public LayerMask whatIsGround; public bool flying = false; public int groundSpeed = 5; public bool flyCollision = false; public float flyingSpeed = 1; void FixedUpdate() { StartCoroutine ("Fly"); Moving (); } private IEnumerator Fly() { yield return new WaitForSeconds (2); FlyUp (); flying = true; moving = false; //grounded = false; groundSpeed = 0; //needsCollision = true; rigidbody2D.gravityScale = -2f; } void Moving() { grounded = Physics2D.OverlapCircle (groundcheck.position, groundRadius, whatIsGround); rigidbody2D.velocity = new Vector2 (transform.localScale.x, 0) * groundSpeed; collision = Physics2D.Linecast (sightStart.position, sightEnd.position, whatIsColliding); Debug.DrawLine (sightStart.position, sightEnd.position, Color.green); if (collision == needsCollision) { this.transform.localScale = new Vector3(( transform.localScale.x == 1) ? -1 : 1,1,1); } } void FlyUp() { flyCollision = Physics2D.Linecast (sightStart.position, flyUpSight.position, whatIsColliding); Debug.DrawLine (sightStart.position, flyUpSight.position, Color.green); if (flying) { if(flyCollision || collision == true) { transform.Translate (Vector3.right * flyingSpeed); if((flyCollision == needsCollision)) { this.transform.localScale = new Vector3(( transform.localScale.x == 1) ? -1 : 1,1,1); } } } } EDIT: so i went ahead and hardcoded it, and now i have a very funny function, it flies up and down and goes around the block void FixedUpdate() { rigidbody2D.velocity = new Vector2 (transform.localScale.x, 0) * speed; collision = Physics2D.Linecast (sightStart.position, sightEnd.position, whatIsColliding); Debug.DrawLine (sightStart.position, sightEnd.position, Color.green); if (collision == needsCollision) { this.transform.localScale = new Vector3 ((transform.localScale.x == 1) ? -1 : 1, 1, 1); } StartCoroutine("FlyUp"); } private IEnumerator FlyUp() { yield return new WaitForSeconds (1); transform.Translate (Vector3.up * 0.1f); yield return new WaitForSeconds (2); transform.Translate (-Vector3.up * 0.5f); yield return new WaitForSeconds(2); StopCoroutine ("FlyUp"); }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>