Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
0
Question by abhishek7 · Jun 23, 2017 at 11:26 AM · 2dmovementrandomquack

Unity 2D Random Movement

Hi everyone, I'm simply trying to make a ball move in random directions (and bounce off the sides of the walls), and this is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyEasyAI : MonoBehaviour {
     public Rigidbody2D rb;
     public float speed = 1;
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody2D> ();
 
     }
     
     void FixedUpdate(){
         Vector2 Movement = new Vector2 (Random.Range(-1, 1), Random.Range(-1, 1));
         rb.AddForce (Movement);
         print (Movement);
 
         
         
     }
 }

However, what happens whenever I execute the script is that the gameobject simply moves down and to the left, and then stays put at the corner (the corner of the wall where it is being blocked by the colliders). How can I edit it so that the ball moves in a random and sporadic fashion? Any help would be appreciated.

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
9
Best Answer

Answer by FlaSh-G · Jun 23, 2017 at 11:39 AM

I'd try setting a random direction every 1-2 seconds or so and then stay with that, and apply force in that direction, until you change it again. If you randomize every (fixed) frame, you don't have enough time to accelerate.

 public float accelerationTime = 2f;
 public float maxSpeed = 5f;
 private Vector2 movement;
 private float timeLeft;
 
 void Update()
 {
   timeLeft -= Time.deltaTime;
   if(timeLeft <= 0)
   {
     movement = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));
     timeLeft += accelerationTime;
   }
 }
 
 void FixedUpdate()
 {
   rb.AddForce(movement * maxSpeed);
 }

Note that I replaced the ints in the Random.Range calls with floats so you can get anything between -1 and 1 instead of just -1, 0, or 1. Your choice, of course.

Comment
Add comment · Show 3 · 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 abhishek7 · Jun 23, 2017 at 01:41 PM 0
Share

Thanks this is exactly what I needed :)

avatar image TheQiwiBoy · Aug 07, 2021 at 03:11 PM 0
Share

Thank you so much! I was trying to make a tornado projectile, and this was perfect for it!

avatar image Bradwall · Nov 25, 2021 at 07:22 PM 0
Share

Thank you so much for this script. i've been trying to make/find one for at least two months now and this works PERFECTLY. Again, thank you.

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

143 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

Related Questions

How to create random movement in 2d 2 Answers

Random movement direction cycle. 2 Answers

Random Movement : 2d 1 Answer

How to create random movement in 2D 2 Answers

Making enemies jump at random (c#) 2 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