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 S_Byrnes · May 13, 2016 at 06:44 AM · c#collisionrandomrigidbody2ddirection

Why is my Rigidbody2D not moving? (C#)

Hi all, I'm wondering if anyone knows what's wrong with my script, I've been experimenting with this for a while, I had it half working with transform.position and a random value for both y and x in the one 'direction' value, but I needed to separate the x and y so I could manipulate the direction it would go when it hit the barriers (Y_Max, Y_Min etc).

Basically I need my objects to bounce off the wall realistically, so if it's moving left and downwards when it hits the Min_Y, I need it to maintain it's X direction but reverse it's Y direction, and so on for the rest of the other walls.

Here is my current code, it doesn't include my other attempts because I feel this is the closest I've gotten, the only issue is they don't move anywhere even on Start().

 Rigidbody2D rb;
 
 public float xRandom;
 public float yRandom;
 
 private float xDirection;
 private float yDirection;
 
 public float Y_Min = -8f;
 public float Y_Max = 8f;
 public float X_Min = -8f;
 public float X_Max = 8f;
 
 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 
     xRandom = Random.Range(-1.0f, 1.0f);
     yRandom = Random.Range(-1.0f, 1.0f);
 
     xDirection = xRandom;
     yDirection = yRandom;
 
 }
 
 void Update()
 {
 
     rb.velocity = (new Vector2(xDirection, yDirection)) * moveSpeed * Time.deltaTime;
 
     if (rb.velocity.y > Y_Max)
     {
         yDirection -= yDirection;
     }
 
     if (rb.velocity.x > X_Max)
     {
         xDirection -= xDirection;
     }
 
     if (rb.velocity.y < Y_Min)
     {
         yDirection -= yDirection;
     }
 
     if (rb.velocity.x < X_Min)
     {
         xDirection -= xDirection;
     }
 }

Thanks all, I appreciate any help you can offer!

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 CBRZY · May 13, 2016 at 07:57 AM

Currently I cannot test your code, but there are a few things that seem a bit off.

Usually you wouldn't change the velocity of a rigidbody directly, rather add a force to the rigidbody to get it to move. http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

You are using Random with values of -1 to 1 to get the direction, which are very small amounts and can random to a value like 0 or 0.1 which you almost won't be able to see when applied to an object that isn't currently moving.

You are using X_Max, X_Min, Y_Max and Y_Min as variables to store "Barriers", but you are not comparing the rigidbody's x and y position to them, you are comparing them to the rigidbody's velocity, which will never be greater or smaller than your min and max values.

Also, just something small, when creating a "barrier", test for equal as well. It is not such a big issue, just something to look out for.

Comment
Add comment · Show 5 · 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 S_Byrnes · May 13, 2016 at 08:18 AM 0
Share

You're right it would choose 0 or other numbers that are slower every now and then, so I've just changed it to an integer ins$$anonymous$$d of a float, but it still chooses 0 sometimes.

Here's what my code looks like now:

 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 
     xRandom = Random.Range(-1, 1);
         yRandom = Random.Range(-1, 1);
 
         xDirection = xRandom;
         yDirection = yRandom;
 
     }
 
     void Update()
 {
 
     Vector2 movement = new Vector2(xDirection, yDirection);
 
     rb.velocity = movement * moveSpeed;
 
     if (rb.position.y >= Y_$$anonymous$$ax)
     {
         yDirection -= yDirection;
     }
 
     if (rb.position.x >= X_$$anonymous$$ax)
     {
         xDirection -= xDirection;
     }
 
     if (rb.position.y <= Y_$$anonymous$$in)
     {
         yDirection -= yDirection;
     }
 
     if (rb.position.x <= X_$$anonymous$$in)
     {
         xDirection -= xDirection;
     }
 }

I'm not sure AddForce is what I need, they're just collectible items that float at a constant rate around the screen, if you know how to do this with AddForce I'd be happy to try it.

$$anonymous$$y new code will hit the walls, but it will just stop against the wall but it will keep it's other directional velocity but it won't bounce off of the wall.

Any suggestions?

avatar image CBRZY S_Byrnes · May 13, 2016 at 08:27 AM 0
Share

I will test this code as soon as I get home, but I have noticed something that is causing the problem.

When your object reaches the barrier it doesn't change the direction it just changes it to 0.

 //This just subtracts the current value with itself
 xDirection -= xDirection; 
 //It should look like this
 xDirection = -(xDirection);

avatar image S_Byrnes CBRZY · May 13, 2016 at 08:32 AM 0
Share

Thanks Brits, cool of you to be helping out while you're out!

It still happens though, even with that method unfortunately. Also they still decide to just stay put a lot of the time, but that's a different part of the code anyway.

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

163 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 avatar image

Related Questions

How to make character stop at wall? 1 Answer

Problem with objects colliding and IsSleeping (C#) 1 Answer

I want to teleport my player to a random(from these 4) spot on the x axis, but it doesn't work. 3 Answers

Multiple Cars not working 1 Answer

How to know from which direction there is a collision with an object? in C# 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