Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TysonG · Apr 01, 2019 at 02:16 AM · transformpositionrandomenemyrectangle

How to move an GameObject to a random position in a defined area?

Hey everyone, so I have this bat enemy that once it hits the player, it is supposed to move to a position back in the trees (the red rectangle) and then start chasing the player again. I'm not quite sure how to define the area for it to reposition to or how to pick a random position in that defined area. I tried some code towards the bottom to see if I could get the enemy to hit the player and at least move to a random position on the screen as a start but I couldn't even get that to work. Any help would be appreciated. alt text

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BatController : Enemy
 {
     Animator animator;
     SpriteRenderer spriteRenderer;
 
     public float speed;                             // speed that bat chases MegaMan
     public float repositionSpeed;
     public float distBetween;
     public float deathDistance;
     public float timeToAwake = 3f;
 
     private Transform player;                       // holds the player
 
     private bool startTimer = false;
     private float timer;
 
     void Start()
     {
         animator = GetComponent<Animator>();
         animator.Play("Bat_idle");
         timer = timeToAwake;
 
         player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();          // get player position
     }
 
     void Update()
     {
         if (Vector2.Distance(transform.position, player.position) <= distBetween)
         {
             startTimer = true;
             if (startTimer)
             {
                 timer -= Time.deltaTime;
                 if (timer <= 0)
                 {
                     animator.Play("Bat_fly");
                     startTimer = false;
                     transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);      // move bat, towards play pos, at certain speed
                 }
             }
         }
 
         if (Vector2.Distance(transform.position, player.position) > deathDistance)
         {
             Destroy(this.gameObject);
         }
     }
 
     private void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag == "Player")
         {
             float moveY = Random.Range(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y, Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y);
             float moveX = Random.Range(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x, Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0)).x);
             Vector2 movePos = new Vector2(moveX, moveY);
             transform.position = Vector2.Lerp(transform.position, movePos, repositionSpeed * Time.deltaTime);
         }
     }
 }

game-opt.jpg (78.1 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

Answer by toddisarockstar · Apr 01, 2019 at 03:10 AM

hmmmm. not sure what is defining your red rectangle. i would assume the easiest way would be to use the bat's start postition???

i'm not sure what the purpose is in NOT deriving this script from monodevelop. the top of your script looks a bit weird. hopefully you know what you are doing with that.

 using UnityEngine;
 using System.Collections;
 
 
 // make sure your script is named "BatController" in the inspector.
 // drag and drop this script onto the bat
 
 public class BatController : MonoBehaviour {
 
     public Vector3 RandomPositionInBox (float width ,float height){
         return new Vector3(
             Random.Range(0f,width)+startpos.x-(width*.5f),
             Random.Range(0f,height)+startpos.y-(height*.5f),
             startpos.z);
 
     }
     
     public Vector3 startpos;
     void Start () {        
                 startpos = transform.position;
         }
       
 }








 
Comment
Add comment · Show 1 · 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 TysonG · Apr 01, 2019 at 01:11 PM 0
Share

The box is just a visual reference and it's nothing in-game. I just added it in photoshop to help you guys visual what I'm talking about.

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

149 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

Related Questions

Enemy position transform 1 Answer

Spawn object at a random predeterminated(transform) position 1 Answer

Return Enemy To Start Position 1 Answer

How to Return Enemy to startPosition 1 Answer

Circle inside Circle 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