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 highpockets · Jul 06, 2013 at 07:33 PM · vector3lerpreflectionvector2reflect

I'm Having Trouble With Vector3.Reflect.. Please Help?

Hello,

I think I'm doing something wrong with Vector3.Reflect and I think it has something to do with the normals, but I'm not quite sure how to fix it. Here is the scenario, while my player object moves, it is being controlled by Vector2.Lerp, I record the last position and second to last position of the player while it Lerps, so when it collides with another object, Vector3.Reflect is passed the second to last position as the vector to reflect, the current position as the inNormal (reflector) and the reflected vector that results from the function is used as the target destination in my Lerp function. The problem is that the reflected vector is popping up in strange spots (always on the left). I'm thinking that it must be that my normals are messed up, I also think that I may have to change my update function to fixed update. Because I'm pretty sure that collisions are processed every physics time step and since I change the tag of the player object to recognize the collision over multiple scripts in oncollisionenter and reference it in update of another script where I implement the reaction as a result of the collision, I think there may be some delay, but I'm not familiar with this stuff.

Hopefully somebody can tell me where I mucked up.

Here is my code:

The collision script attached to the player object:

 public class CraneCollisions : MonoBehaviour
 {
 
     void OnCollisionEnter(Collision other)
     {
         
         gameObject.tag = "Hit";
     }
 }


The Gameplay script:

 private float speed = 3f;
 Vector2 cranePos = crane.transform.position;
 Vector2 targetLocation;
 Vector3 lastPos;
 Vector3 secondToLastPos;
 
 void Update()
 {
     
     if( crane.tag == "Hit" )
     {
             
         targetLocation = Vector3.Reflect( secondToLastPos, crane.transform.right );
         crane.transform.position = Vector2.Lerp( cranePos, targetLocation, Time.deltaTime * speed );

         if( targetLocation == crane.transform.position );
         {
 
             crane.tag = "Player";
         }
 
     }
 
     if( crane.transform.position != lastPos )
     {
             
         secondToLastPos = new Vector3( lastPos.x, lastPos.y, 0 );
         lastPos = new Vector3( crane.transform.position.x, crane.transform.position.y, 0 ) ;
     }
 }

***Please note that I didn't include any other part of the script than the subjects that I want to touch on because it is very long. Thanks

Any help would be much 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
1
Best Answer

Answer by robertbu · Jul 06, 2013 at 09:15 PM

Your 'inDirection' is calculated wrong. It should be (lastPos - secondToLastPos). You are calculating a vector using a position, which means the vector will be from the world origin. You want the vector to be relative to object moving. As for the normal, 'crane.transform.right' does not look right, but might work if you have special knowledge of the geometry. What you are looking for is a vector perpendicular to the surface of the object you are bouncing off of at the point of the hit...in this case the player object. Typically you would get the normal from the contact points of the collision pass into OnCollisionEnter(). Typically you would have the the OnCollisionEnter() in the same moving object you would want to reflect. Sometimes you can get the normal from a Raycast(). The RaycastHit that is set during the call contains the normal of the surface at the point hit. For the special case of a collision between two spheres, you can get the normal by subtracting one position from another.

Note what you get back from Vector3.Reflect() will be a vector, not a ray. You will still need to calculate the position:

 targetLocation = Vector3.Reflect(/* whatever parameters you use */);
 targetLocation = lastPos + targetLocation * distance;
Comment
Add comment · Show 2 · 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 highpockets · Jul 06, 2013 at 11:14 PM 0
Share

Hey, thanks for that. I'm getting a NullReferenceException now..

I changed my inDirection to ( lastPos - secondToLastPos ), which seemed to change something in a positive way, but I tried to get the contact point normal, which seems to be presenting itself in the oncollisionenter script with no trouble, and I used the variable in my gameplay script and I'm getting a NullReferenceException...

         if( crane.tag == "Hit" )
         {
             
             Vector3 inDirection = lastPos - secondToLastPos;
             
             targetLocation = Vector3.Reflect( inDirection, craneCollisions.contactNormal );
             Debug.Log(targetLocation);
             Lerping( targetLocation, $$anonymous$$oveType.Fast );
         }

The NullReferenceException is popping up on the Vector3.Reflect line. I didn't get it before I changed the inNormal to the vector from the collision script. Do you know what I'm doing wrong?

avatar image robertbu · Jul 07, 2013 at 02:36 AM 0
Share

You would get a nullReferenceException if 'craneCollisions' was not initialized. You don't show the rest of your code, so I cannot see how you are trying to initialize this variable.

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

15 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

Related Questions

Problem with lerping Y axis 0 Answers

Vector3.Lerp result in "laggy" movement while running on iOS devices 2 Answers

[newbie] Vector3.Reflect not working properly. Please help 0 Answers

Set speed by distance from object 1 Answer

Moving the player toward a direction 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