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 LukeThePunk666 · Feb 20, 2020 at 06:15 PM · random2d-platformermovement scriptcrashing

Why does this script crash Unity? Also simple random 2D movement?

For some reason this script crashes Unity when I run my game. I'm trying to get some simple random 2D movement. This is my first time using coroutines so I'm sure I'm doing it wrong but I didn't think I was doing it so wrong to get Unity to crash lol. Anyone know what's wrong and also anybody know how to get my script doing what I want it to do?

 {
     Rigidbody2D rb;
     public float moveSpeed = 10f;
     Vector2 currentPos;
     Vector2 targetPos;
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         StartCoroutine(ghostMoveRandom());
     }
 
     IEnumerator ghostMoveRandom()
     {
         yield return new WaitForSeconds(2);
         currentPos = new Vector2(rb.position.x, rb.position.y);
         targetPos = Random.insideUnitCircle;
         while(currentPos != targetPos)
         {
             rb.transform.Translate(targetPos);
         }
         
         StartCoroutine(ghostMoveRandom());
     }
 }
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
3
Best Answer

Answer by rh_galaxy · Feb 20, 2020 at 06:36 PM

The most obvious problem is that most likely currentPos will never equal targetPos, because you are never changing currentPos. I guess that you will want to move towards targetPos over several frames.

I would solve this in Update() instead to avoid coroutines, but some prefere them. (Untested code)

     Rigidbody2D rb;
     float timer = 0;
     float speed = 10;
     Vector2 currentPos;
     Vector2 targetPos;
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         targetPos = rb.position;
     }
     void Update()
     {
         currentPos = rb.position;
         timer += Time.deltaTime;
         if (timer >= 2.0)
         {
             targetPos = Random.insideUnitCircle;
             timer = 0;
         }
         rb.MovePosition(Vector2.MoveTowards(currentPos, targetPos, speed * Time.deltaTime));
     }
 
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 LukeThePunk666 · Feb 21, 2020 at 09:56 AM 0
Share

Thank you, this worked perfectly. :)

avatar image LukeThePunk666 · Feb 21, 2020 at 10:13 AM 0
Share

Okay so your script works well, but for some reason the ghosts' first target position is always a single point near the beginning of the level, regardless of where I place them, and then they will start their random motion when they've reached that place. Any ideas? Also if you think I should just post this as a separate question then I'll do that :)

avatar image rh_galaxy LukeThePunk666 · Feb 21, 2020 at 12:42 PM 0
Share

Hmm, the first two sec loop it should just stand still since currentPos and targetPos are the same, then to get movement that makes it wander around more you could maybe do

 targetPos = currentPos + Random.insideUnitCircle * 2.0;

But not sure I understand.

avatar image LukeThePunk666 rh_galaxy · Feb 21, 2020 at 01:04 PM 0
Share

Hey thanks for the response, I suppose I didn't explain properly. It does stay still for the first 2 secs, and when it goes to find a location for the first time, it will exceed the boundaries of the circle it should be moving in and will always travel far away to this one specific spot. Once there, it will start moving around within the boundaries of the circle as I would expect.

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

139 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

Related Questions

how to have object fall using random speed 0 Answers

Random crash in ntdll.dll 0 Answers

Player Dash doesn't work properly 0 Answers

How to fix my movement? 0 Answers

Unity Webplayer Random Crashing 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