• 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 mayonez · Sep 03, 2014 at 09:09 PM · 2dcollisiontriggerrigidbody2dvelocity

Rigidbody2D.velocity out of controll

Hi Guys!

I'm quite new to Unity (I'm working on my first project). In my game, I have a reactangle area, in which there's a couple of balls. These balls aren't affected by gravity, nor friction. I only add them a force at launch and then I set them free.

But, Unity's built-in colliders are a bit too realistic for my game. Instead of four edge colliders, I use four very narrow trigger box colliders, with a really simple script. F.e. if a ball enters the upper trigger i change it's velocity from [x, y] to [x, -y], and so on.

And generally, everything's working nearly fine. But, as the game goes on, some balls just disappear! They approach a trigger, enter it (checked with a debug), their velocity is changed properly (also checked), but then, somehow (with no collision or antyhing like that), a few frames later, these velocities change to some random values that make them leave the screen. I don't know, if it's my fault, or maybe Unity's physics engine ;), but i certainly need Your help!

Best regards, mayonez

Comment
Add comment · Show 3
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 Pyrian · Sep 03, 2014 at 10:16 PM 1
Share

I would guess that the cause of your problem is not anything you've told us so far. Velocity shouldn't change without something somehow modifying it, and the challenge is going to be finding out what's modifying it. And I'm sorry, but it's most likely your own code, somewhere, somehow.

avatar image Malfegor · Sep 04, 2014 at 06:37 PM 0
Share

So you've debugged these values? If they're through the roof, why not try something like:

 float $$anonymous$$axValue = 10.0F;
 
 rigidbody2D.velocity = new Vector2($$anonymous$$athf.Clamp(rigidbody2D.velocity.x, 0, $$anonymous$$axValue), $$anonymous$$athf.Clamp(rigidbody2D.velocity.y, 0, $$anonymous$$axValue));

That'll (probably) make sure the values do not go above $$anonymous$$axValue (10 in this case)... Hope it helps!

avatar image mayonez · Sep 04, 2014 at 07:04 PM 0
Share

Well, that's not really my problem. Sorry if my description was not clear, but I do not need to controll the magnitude, but the vector (both x and y values), because this actually causes balls to leave the camera area.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by brycem24 · Sep 03, 2014 at 10:20 PM

Triggers do not block colliders. You could add both a box collider, and a box trigger to handle this. If this somehow screws up your script. I would verify that you have your script applied to all of the trigger areas. If this causes problems for the bottom ones perhaps I would change the velocity from x,y to according to which side it is on. Bottom remaining +Y. I just remembered in typing this for another thing you should watch out for. I believe in my experience with 2D. Y is not used everything is X and Z so I would watch out for that in your trigger script aswell. This could be when the ball touches it the velocity is changed so rather than the ball touching the collider and going back down its actually going to the side and past your camera giving the illusion of it dissapearing.

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 mayonez · Sep 04, 2014 at 06:25 PM 0
Share

Thank You for answering! I know that triggers do not block colliders, but my script attached to those triggers should, and it does, in most cases. I'm 100% sure, the script is attached to all four bounds of my area. Probably it doesn't really matter, whether 2D uses Y or Z, beacuse I always use Vector2s, so I just operate on the second component, which is this Y or Z.

avatar image
0

Answer by LSPressWorks · Sep 03, 2014 at 10:36 PM

You can always cap the speeds in code with clampf or a couple of simple if statements to measure the velocity and adjust accordingly.

if(this.rigidbody.velocity.magnitude > someNumber) {//then do stuff

}

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 mayonez · Sep 04, 2014 at 06:28 PM 0
Share

Thanks for Your answer! But I fear, that's not a solution for my problem :/ I don't need to regulate the magnitude, but the vector. It is just somehow changed to a vector, that leads the ball out of bound, what never should happen...

avatar image Pyrian · Sep 04, 2014 at 06:56 PM 0
Share

How much code is in this project? I think we might need to see it all.

avatar image mayonez · Sep 04, 2014 at 07:02 PM 0
Share

I've posted an answer with a full description of a testing-project, where the same problem occurs, but it's still waiting for a moderator.

avatar image
0

Answer by mayonez · Sep 05, 2014 at 09:44 AM

Okay, I read Your answers and thought that it's probably my code's fault. So I decided, to cut this part of my game, and put into a separate, testing project. Now, this testing project contains only two real object - bounds, and balls. I've attached the following script to the bounds (I set all four Mods depending on the position of a bound - whether it's top, bottom, left or right):

 using UnityEngine;
 using System.Collections;
 
 public class BoundScript : MonoBehaviour {
 
     public int xMod, yMod, xPosMod, yPosMod;
 
     void Start() {
         Vector2 stw = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
         transform.position = new Vector2(stw.x * xPosMod, stw.y * yPosMod);
     }
 
     void OnTriggerEnter2D(Collider2D c) { 
         c.attachedRigidbody.velocity = new Vector2(c.attachedRigidbody.velocity.x * xMod, c.attachedRigidbody.velocity.y * yMod);
     }
 
 }

And this one to all of the balls:

 using UnityEngine;
 using System.Collections;
 
 public class ObjectColliderController : MonoBehaviour {
 
     void Start() {
         rigidbody2D.AddForce(new Vector2(35, 35));
     }
 
 }
 

There's literally nothing (apart from the camera) on the scene, but the problem remains the same...

Thanks for all help, mayonez.

Comment
Add comment · Show 9 · 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 Pyrian · Sep 05, 2014 at 04:03 PM 0
Share

Are you sure the ball doesn't hit the same trigger twice? $$anonymous$$aybe try making them really wide ins$$anonymous$$d of super narrow.

avatar image mayonez · Sep 05, 2014 at 05:57 PM 0
Share

Yes, I'm sure it doesn't hit twice. I checked with a debug and actually this random velocity change does not act like my scrpit. I mean, when the velocity vector is f.e. [2, 2] and the ball enters the upper trigger, the velocity is changed to [2, -2], so it's done properly. But then, a few frames later, it's changed to [0.5, 1.6] and the ball leaves the screen. That's how it looks like.

avatar image mayonez · Sep 05, 2014 at 06:03 PM 0
Share

Ok, I've just made the triggers wider, just in case, but it gave nothing.

avatar image Pyrian · Sep 06, 2014 at 05:23 AM 0
Share

I can't duplicate this. I copied your code into a little scene, and it worked just fine; nothing escaped, nothing even behaved weird.

avatar image mayonez · Sep 06, 2014 at 09:51 AM 0
Share

But have You tried to put more than one ball in the scene? For one ball it works fine also for me, but if there's about 40, it doesn't.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character slows down when running against a wall (2D) 1 Answer

OnTriggerEnter2D problem on created collider2d and rigidbody2d during runtime 0 Answers

Troublesome collision detection when changing rigidbody.velocity directly 0 Answers

Unity C# 2D Adding Velocity on rotation 1 Answer

Convert Force into Velocity for 2D Player Jump 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges