Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 pepperedereppep · Apr 09, 2011 at 02:40 PM · collisionreflectionvectormirrornormal

exact vector reflection along collision normal vector

Alright, I have a rigidbody box bouncing against two walls on each side horizontally. Right now I have it so that once the box hits the wall, it'll detect the normal vector of the collision and use vector3.reflect to move in the mirrored direction. Here's the code:

private Vector3 velocity; private float speed;

void Awake () {

 velocity = transform.forward;
 speed = 10;

}

void FixedUpdate () { transform.position += velocity Time.deltaTime speed; }

void OnCollisionEnter(Collision wall) { foreach(ContactPoint contact in wall.contacts) { velocity=Vector3.Reflect(velocity, contact.normal); return; } }

The problem here is that, because I had no choice but to have the object be non-kinematic, the square ends up moving a tiny bit by itself.. My question is, how do I have it so that the the mirror reflection is perfectly relative to the collision normal vector every time, preventing it from moving arbitrarily after a while. Any help is appreciated.

Note: I don't want the object to go back and forward forever, so I'm not looking for a pingpong code between two positions. Main thing that i want is for the motion to be exactly a reflection relative to the contact normal every time.

EDIT: here's the file to better illustrate what i'm talking about:

http://www.speedyshare.com/files/27869658/mirrorVector2.rar

press play and watch as the box begins to slowly move up or down. this is the problem i'm talking about..

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 Dani · Apr 09, 2011 at 02:55 PM 0
Share

Rounding errors? no cure for that in the foreseeable future.

avatar image Peter G · Apr 09, 2011 at 03:09 PM 0
Share

You won't receive collision information by moving your transform like that. Unity doesn't send collision info when you directly modify the transform.

avatar image pepperedereppep · Apr 09, 2011 at 11:13 PM 0
Share

basically my question is, why is the cube moving up and down when if i log the velocity and the reflected velocity, all i'm getting is -1 and 1 on the z axis? and if there's a way to stop this from happening

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Mike 3 · Apr 09, 2011 at 03:18 PM

I think this one was fixed on irc, the issues were with moving with transform, and then multiple contact points when moving the cube:

using UnityEngine; using System.Collections;

public class mirrorVector : MonoBehaviour {

 private Vector3 velocity;
 private float speed;


 void Awake () {

     velocity = transform.forward;
     speed = 2;

 }

 void FixedUpdate () {

     rigidbody.MovePosition(rigidbody.position + velocity * Time.deltaTime * speed);
 }

 void OnCollisionEnter(Collision bla) 
 {
     foreach(ContactPoint contact in bla.contacts)
     {
     velocity = Vector3.Reflect(velocity, contact.normal);
         return;
     }
 }

}

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

How to detect if target is inside an "arc" area? 0 Answers

Finding collision force not working in certain situations. 0 Answers

Collision normal angles. 1 Answer

OnCollisionEnter question 1 Answer

Mirror effect over more than one surface/plane 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