Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bob1234 · Aug 20, 2014 at 02:25 PM · c#aienemy

need help making a simple ascending enemy

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");
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

EnemyAI C# Error 1 Answer

Raycast moving and if null statements 0 Answers

Looping Between Waypoints 1 Answer

How do I get my emeny AI to detect if he sees a player, or one of the good AI? (C#) 3 Answers

[I REALLY NEED HELP FAST]Help with enemy Shooting 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges