Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 twinnightmare · Jun 24, 2010 at 04:43 AM · physicsvector3

Vector3.Reflect problems

When I use vector3.reflect some times the ball will start rolling along the surface that it collided with. I think it is only when the angle is real low.

my reflection code

void OnCollisionEnter(Collision collider) { foreach (ContactPoint contact in collider.contacts) {

                 ballVelocity = Vector3.Reflect(ballVelocity, contact.normal);
         }
     }

my movement code

if ( setting.GetComponent<settings>().stage == stages.game && setting.GetComponent<settings>().ballpaused == false) { if (Physics.Linecast( setting.GetComponent<settings>().vect1, setting.GetComponent<settings>().vect4, mask.value)) {

     ballVelocity = Vector3.Reflect(ballVelocity,Vector3.right);
 }
 if (Physics.Linecast( setting.GetComponent&lt;settings&gt;().vect1,   setting.GetComponent&lt;settings&gt;().vect2, mask.value))
 {
     ballVelocity = Vector3.Reflect(ballVelocity, Vector3.down);
 }
 if (Physics.Linecast( setting.GetComponent&lt;settings&gt;().vect2,   setting.GetComponent&lt;settings&gt;().vect3, mask.value))
 {
     ballVelocity = Vector3.Reflect(ballVelocity, Vector3.left);
 }
 if (Physics.Linecast( setting.GetComponent&lt;settings&gt;().vect4,   setting.GetComponent&lt;settings&gt;().vect3, mask.value))
 {
     Destroy(gameObject);
 }
 ballVelocity.z = 0;
 Vector3 vect = transform.position += ballVelocity * (ballSpeed +   setting.GetComponent&lt;settings&gt;().difficulty) * Time.deltaTime ;

     vect.z = 0;
     if (vect.y &lt;   setting.GetComponent&lt;settings&gt;().vect4.y)
     {
         Destroy(gameObject);
     }
     transform.position = vect;
     transform.position.Normalize();

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by twinnightmare · Jul 08, 2010 at 03:32 AM

With other method still had some problems so cooked up another method that used manual direction calculation.

My Directions

        directions[0] =Vector3.up;
        directions[1] =Vector3.down;
        directions[2] =Vector3.Lerp(Vector3.up,Vector3.left, 0.25f);//
        directions[3] =Vector3.Lerp(Vector3.up,Vector3.left, 0.5f);
        directions[4] = Vector3.Lerp(Vector3.up,Vector3.left, 0.75f);
        directions[5] = Vector3.Lerp(Vector3.left,Vector3.down,0.25f);
        directions[6] = Vector3.Lerp(Vector3.left,Vector3.down,0.5f);
        directions[7] =Vector3.Lerp(Vector3.left,Vector3.down,0.75f);
        directions[8] =-Vector3.Lerp(Vector3.up,Vector3.left, 0.25f);
        directions[9] =-Vector3.Lerp(Vector3.up,Vector3.left, 0.5f);
        directions[10] = -Vector3.Lerp(Vector3.up,Vector3.left, 0.75f);
        directions[11] = -Vector3.Lerp(Vector3.left,Vector3.down,0.25f);
        directions[12] = -Vector3.Lerp(Vector3.left,Vector3.down,0.5f);
        directions[13] =-Vector3.Lerp(Vector3.left,Vector3.down,0.75f);

I omitted the left and right directions b/c didn't need them in my game.

Changed

Vector3 vect = transform.position += ballVelocity * (ballSpeed + setting.GetComponent<settings>().difficulty) * Time.deltaTime ;

TO: if(transform.gameObject.rigidbody != null) {

             transform.gameObject.rigidbody.velocity = ballVelocity * (ballSpeed +   setting.GetComponent&lt;settings&gt;().difficulty) ;
         }

uses the physics engine to do the movement like others suggested, but have to make sure to set velocity back to zero to stop the object when it doesn't need to move.

and my direction recalculating function

private Vector3 bounce(Vector3 Direction, Vector3 normal) {

 if (normal == Vector3.up)
 {
     if (Direction == directions[1])
     {
         Direction = directions[0];
     }
     else if (Direction == directions[8])
     {
         Direction = directions[13];
     }
     else if (Direction == directions[9])
     {
         Direction = directions[12];
     }
     else if (Direction == directions[10])
     {
         Direction = directions[11];
     }
     else if (Direction == directions[7])
     {
         Direction = directions[2];
     }
     else if (Direction == directions[6])
     {
         Direction = directions[3];
     }
     else if (Direction == directions[5])
     {
         Direction = directions[4];
     }
 }
 else if (normal == Vector3.down)
 {
     if (Direction == directions[0])
     {
         Direction = directions[1];
     }
     else if (Direction == directions[13])
     {
         Direction = directions[8];
     }
     else if (Direction == directions[12])
     {
         Direction = directions[9];
     }
     else if (Direction == directions[11])
     {
         Direction = directions[10];
     }
     else if (Direction == directions[2])
     {
         Direction = directions[7];
     }
     else if (Direction == directions[3])
     {
         Direction = directions[6];
     }
     else if (Direction == directions[4])
     {
         Direction = directions[5];
     }

 }
 else if (normal == Vector3.left)
 {
     if (Direction == directions[13])
     {
         Direction = directions[2];
     }
     else if (Direction == directions[12])
     {
         Direction = directions[3];

     }
     else if (Direction == directions[11])
     {
         Direction = directions[4];
     }
     else if (Direction == directions[10])
     {
         Direction = directions[5];
     }
     else if (Direction == directions[9])
     {
         Direction = directions[6];

     }
     else if (Direction == directions[8])
     {
         Direction = directions[7];
     }

 }
 else if (normal == Vector3.right)
 {
     if (Direction == directions[2])
     {
         Direction = directions[13];
     }
     else if (Direction == directions[3])
     {
         Direction = directions[12];
     }
     else if (Direction == directions[4])
     {
         Direction = directions[11];
     }
     else if (Direction == directions[5])
     {
         Direction = directions[10];
     }
     else if (Direction == directions[6])
     {
         Direction = directions[9];
     }
     else if (Direction == directions[7])
     {
         Direction = directions[8];
     }

 }
 else
 {
     Direction = -Direction;
     error = normal;
 }
 return Direction;

}

Comment
Add comment · 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
0

Answer by Tetrad · Jun 24, 2010 at 04:59 AM

Shot in the dark: Could you be hitting multiple contact points at once and not doing the correct reflect calculation?

Is your movement code in FixedUpdate? Does your object have a rigid body on it? Is your object penetrating the collision and getting stuck?

Comment
Add comment · Show 6 · 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 twinnightmare · Jun 25, 2010 at 01:30 AM 0
Share

Yes the movement does happen during the FixedUpdate. Yes it does have a RigidBody attached.

avatar image Tetrad · Jun 25, 2010 at 01:44 AM 0
Share

Not the same kind of question, but I wrote an answer here for somebody doing something similar with movement that you might want to look into. http://answers.unity3d.com/questions/13402/getting-maze-walls-using-3d-meshes-to-work/13418#13418

avatar image twinnightmare · Jun 25, 2010 at 02:11 AM 0
Share

Forgot to mention also using linecasting to detect the collision for the camera boundary.

avatar image twinnightmare · Jun 25, 2010 at 04:16 AM 0
Share

The multiple contact point wouldn't be the problem b/c it uses linecasting on the walls. I think it is more then likely a low angle with the wall. I am just not sure how to make the $$anonymous$$imum angle be higher when i reflect the ball. If that makes any since

avatar image Tetrad · Jun 25, 2010 at 04:31 AM 0
Share

Only suggestion I have is to put some debug lines in your world so you can see what your velocity is, normals are, etc. to make sure things are doing what you think they're doing. http://unity3d.com/support/documentation/ScriptReference/Debug.DrawLine.html

Show more comments
avatar image
0

Answer by Wolfram · Aug 16, 2010 at 03:32 PM

"... some times the ball will start rolling along the surface that it collided with. I think it is only when the angle is real low."

Actually, this effect can be controlled by an editor setting, Edit->ProjectSettings->Physics->BounceThreshold. The default is 2, which is very large. Use something small, adapt to your needs (not too small, because (taken from the docs): "Bounce Threshold Two colliding objects with a relative velocity below this value will not bounce. This value also reduces jitter so it is not recommended to set it to a very low value."

But I admit it is very hard to make the connection that a parameter like this could be the problem - took me a while to get it when I first had the problem you described :-/

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Question on Vector3.Reflect() 2 Answers

When dragging a Door open using RIGIDBODYSCRIPT Plays sound. 1 Answer

Determine Area using code 1 Answer

How to keep physics consistent? 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