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 noahl_unity · Jun 15, 2020 at 07:49 AM · rotationobjectenemy ai

How to make an enemy that walks around a platform?

Hi, I have a quick question. In this game that I'm making I have a plan for an enemy that walks over and walks around platforms (like the buzzy beetle from Mario, it can walk sideways and upside down along a platform). I thought that it would be a menial task, but it turned out to be more complicated than I would like for it to be.

In my code I have devised a way for the enemy to rotate 90 additional degrees (in the clockwise direction) and there are raycasts in place to detect walls and the ground. My issue is that the way that I have the enemy detecting corners is unintuitive. Right now, the enemy ends up a little farther from the corner after each turn so it can't do more than one rotation. My questions is how do I go about what I'm looking for from here? I'm thinking right now I set its transform with Mathf or something similar. I was trying to use the physics system to my advantage but I had no luck.

By the way the enemy does not follow gravity atm.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Alpha : EnemyController
 {
     public LayerMask groundLayer;
     public BoxCollider2D groundCheck2;
     private Rigidbody2D rb;
     private bool rotated;
     public Vector2 norm;
     public float zed;
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         rotated = false;
         zed = transform.eulerAngles.z;
     }
     
     private bool WallInFront()
     {
         RaycastHit2D raycastHit = Physics2D.Raycast(transform.position, transform.right, .5f, groundLayer);
         if (raycastHit.collider != null)
         {
             Debug.DrawRay(transform.position, transform.right, Color.green, .5f);
         }
         else
         {
             Debug.DrawRay(transform.position, transform.right, Color.red, .5f);
         }
         return raycastHit.collider != null;
     }
 
     private bool Grounded()
     {
         float extraHeight = .1f;
         RaycastHit2D raycastHit = Physics2D.BoxCast(groundCheck2.bounds.center, groundCheck2.bounds.size, 0f, -transform.up, extraHeight, groundLayer);
         if (raycastHit.collider != null)
         {
             norm = raycastHit.normal;
             Debug.DrawRay(raycastHit.point, raycastHit.normal, Color.black);
         }
         return raycastHit.collider != null;
     }
 
     // Update is called once per frame
     void Update()
     {
         zed = Mathf.Round(zed / 90) * 90;
         
     }
 
     private void FixedUpdate()
     {
         if (Grounded() && !WallInFront())
         {
             if (zed == 0)
             {
                 rb.velocity = new Vector2(speed * Time.deltaTime, rb.velocity.y);
             }
             else if (zed == 270)
             {
                 rb.velocity = new Vector2(rb.velocity.x, -speed * Time.deltaTime);
             }
             else if (zed == 180)
             {
                 rb.velocity = new Vector2(-speed * Time.deltaTime, rb.velocity.y);
             }
             else if (zed == 90)
             {
                 rb.velocity = new Vector2(rb.velocity.x, speed * Time.deltaTime);
             }
         }
         else if (!Grounded() && !WallInFront() && !rotated)
         {
             RotateDown();
         }
         else if (Grounded() && WallInFront())
         {
             RotateUp();
         }
         
 
     }
 
     void RotateDown()
     {
         rb.velocity = Vector2.zero;
         Quaternion newRot = Quaternion.Euler(0, 0, zed - 90);
         transform.rotation = Quaternion.Slerp(transform.rotation, newRot, 1f);
     }
 
     void RotateUp()
     {
         rb.velocity = Vector2.zero;
         Quaternion newRot = Quaternion.Euler(0, 0, zed + 90);
         transform.rotation = Quaternion.Slerp(transform.rotation, newRot, 1f);
     }
 
 }
 

Any help would be greatly appreciated! If my question is unclear also be sure to let me know!

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

173 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do I keep each cloned object at a certain point after trigger 0 Answers

Slowly Change One Object's Rotation To Match Another's 2 Answers

How to rotate an object to "sit" on a sphere 1 Answer

Help, please with rotation input 0 Answers

Control over y-axis limit in rotating object by touch 0 Answers


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