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 talyon · May 25, 2014 at 11:37 AM · movementrandomenemydirection

random direction of enemy when collide whit wall

hello, I'm trying to move the enemies move randomly until they touch the wall and then again with random directions.

this is the script that i used for the random direction

 public float speed = 5.0f;
 public Vector3 direction;

 void Start() 
 {
     direction = (new Vector3(Random.Range(-1.0f,1.0f), Random.Range(-1.0f,1.0f),0.0f)).normalized;
     transform.Rotate(direction);
 }
 
 void Update()
 {
     transform.position += direction * speed * Time.deltaTime;
 }

 void OnCollisionEnter (Collision col)
 {
     if (col.gameObject.tag == "Muri")        
     {
         random ();
     }
 }

 void random ()
 {
     Vector3    direction = (new Vector3(Random.Range(-1.0f,1.0f), Random.Range(-1.0f,1.0f),0.0f)).normalized;
     transform.Rotate(direction);
 }

should be doing movements similar to the wandered of geometry wars and sometimes it works but many times when it touches the wall continues to move in that direction and I can not understand why, I made you a picture to make you understand what succedde sometimes when touching the wall. alt text

this is as it should be, and in fact every now and then it works but not always.

alt text

Thanks in advance!

how should be.png (7.8 kB)
enemy movement.png (4.8 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · May 25, 2014 at 02:58 PM

You can get the normal of the surface and then do a random angle axis rotation to get the new direction.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour {
     public float speed = 5.0f;
     public Vector3 direction;
     
     void Start() 
     {
         direction = (new Vector3(Random.Range(-1.0f,1.0f), Random.Range(-1.0f,1.0f),0.0f)).normalized;
         transform.Rotate(direction);
     }
     
     void Update()
     {
         Vector3 newPos = transform.position + direction * speed * Time.deltaTime;
         rigidbody.MovePosition (newPos);
     }
     
     void OnCollisionEnter (Collision col)
     {
         Debug.Log ("Collision");
         if (col.gameObject.tag == "Muri")   
         {
             direction = col.contacts[0].normal;
             direction = Quaternion.AngleAxis(Random.Range(-70.0f, 70.0f), Vector3.forward) * direction;
             transform.rotation = Quaternion.LookRotation(direction);
         }
     }
 }
Comment
Add comment · Show 7 · Share
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
avatar image talyon · May 25, 2014 at 08:52 PM 0
Share

I tried but it does not work, does exactly as before.

avatar image robertbu · May 26, 2014 at 01:53 AM 0
Share

I edited the code. This code is tested and works for my simple example. The issue with my original code was I did not set 'direction'. I made a few other changes as well.

avatar image talyon · May 26, 2014 at 06:52 AM 0
Share

oh yes! now it works perfectly, thanks a lot!!

avatar image talyon · May 26, 2014 at 08:00 AM 0
Share

I don't know why but sometimes the enemy through the wall and inevitably move away from the scene. Through it, ignoring the collider. does not happen often but once in a hundred it and do not understand why.alt text

immagine.png (6.6 kB)
avatar image talyon · May 26, 2014 at 08:03 AM 0
Share

I'm noticing that every time it happens there is a drop in frame rate, so it is not that at the time of the fall of the collision frame and fails to calculate and passes through?

Show more comments

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

20 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

Related Questions

Unity2D, How do i check which way a sprite is facing? 1 Answer

Enemy moves randomly within a area. 1 Answer

Need player to walk in random direction 2 Answers

2D Random AI 1 Answer

Enemy moving left towards player. 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