Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Afreshstart · May 26, 2021 at 07:16 PM · transform.positionrandom.range

Please help with 2d 8 way random enemy movement within a range

Hi I am new to unity having and having a hard time finding info on this exact problem. I was thinking of using an array so I can do right+up diagonals for example but cannot figure out how to keep the monsters within a range if I use one. There's some code I really like but I'm having a hard time putting it together properly and am not sure it's even compatible. Here is the part I want to use instead of the random angles..

Vector2[] moveDirections = new Vector2[] { Vector2.right, Vector2.left, Vector2.up, Vector2.down, Vector2.right + Vector2.down, Vector2.left + Vector2.down, Vector2.left + Vector2.up, Vector2.right + Vector2.up };

This is the best code I could find but it doesn't have the exact angles or include rigidbodies (you need these for when enemies are hit right?) and I'm not sure I can use the array with it. I might just use it if I have to.. It used to have $$ Symbols that I changed because I cannot get unity to work otherwise..

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Enemy : MonoBehaviour { public float waitTime = 2f; public float directionChangeTime = 3f;

 private float latestDirectionChangeTime;
 private float characterVelocity = 10f;
 private Vector2 movementDirection;
 private Vector2 movementPerSecond;
 private bool isWaiting;
 int minX = -8;
 int maxX = 8;
 int minY = -4;
 int maxY = 4;

 protected void Start()
 {
     isWaiting = false;
     latestDirectionChangeTime = 0f;
     CalcuateNewAnonymousMovementVector();
 }

 protected void Update()
 {
     
     if (Time.time - latestDirectionChangeTime > directionChangeTime && !isWaiting)
     {
         isWaiting = true;
         Invoke("InvokeIsWaiting", waitTime);
         movementPerSecond = Vector3.zero;
     }

     //move enemy: 
     transform.position = new Vector2(transform.position.x + (movementPerSecond.x * Time.deltaTime),
     transform.position.y + (movementPerSecond.y * Time.deltaTime));

// This was really useful to keep stuff in the boundaries and not be stuck moving against the wall like with the clamp so would really like to keep it if possible..

     if (transform.position.x < minX || transform.position.x > maxX) movementPerSecond.x = -movementPerSecond.x;
     if (transform.position.y < minY || transform.position.y > maxY) movementPerSecond.y = -movementPerSecond.y;

 }

 void InvokeIsWaiting()
 {
     latestDirectionChangeTime = Time.time;
     CalcuateNewAnonymousMovementVector();
     isWaiting = false;
 }

 void CalcuateNewAnonymousMovementVector()
 {
     
     movementDirection = new Vector2(UnityEngine.Random.Range(-1.0f, 1.0f), UnityEngine.Random.Range(-1.0f, 1.0f)).normalized;
     movementPerSecond = movementDirection * characterVelocity;
 }

}

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

162 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

Related Questions

Check if the position taken 1 Answer

How to instantiate a platform each time another platform is 3 units away from the gamecontroller? 0 Answers

¿como instanciar una plataforma cada vez que otra plataforma este a 3 unidades de distancia del gamecontroller? 0 Answers

Random move,Random move and rotation 0 Answers

Coins are not spawning but the life is spawning? 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