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 /
  • Help Room /
avatar image
0
Question by siddharth3322 · Jan 30, 2017 at 02:00 PM · rotationcollisionphysics2dcollision detection

According to Collision Change Rotation of Car

Basically I want to give rotation to player car as per collision occur with wall. Following image gives you over all idea.

alt text

I have following code that giving me correct collision reflection but its in position form not for rotation.

 void OnCollisionEnter2D (Collision2D other)
     {
         ContactPoint2D contact = other.contacts [0];
         Vector3 reflectedVelocity = Vector3.Reflect (direction, contact.normal);      
         direction = reflectedVelocity;
     }

What I have to do for getting correct collision rotation for car? As well smooth turn for car on same rotation so car start moving on that way.

Please give me some help for this.

screen-shot-2017-01-30-at-71151-pm.png (43.4 kB)
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
Best Answer

Answer by siddharth3322 · Jan 31, 2017 at 04:48 PM

For this help, thanks to @UriPopov

Here you have correct answer for this point:

  using UnityEngine;
 
 public class Test : MonoBehaviour {
     Vector3 velocity;
     float speed = 5;
     float rotationSpeed = 5;
     void Start()
     {
        // velocity = -transform.up;// This just makes the my cube "fall" in my test scene
          velocity = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), 0f);
     }
     void Update()
     {
         if (velocity != Vector3.zero)
         {
             //transform.rotation = Quaternion.Slerp(
             //    transform.rotation,
             //    Quaternion.LookRotation(velocity),
             //    Time.deltaTime * rotationSpeed
             //);
             float angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.AngleAxis(angle -90, Vector3.forward),Time.deltaTime*rotationSpeed);
         }
         transform.position += velocity * Time.deltaTime * speed;
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         ContactPoint2D contact;
         contact = other.contacts[0];
         velocity = 2 * (Vector3.Dot(velocity, Vector3.Normalize(contact.normal))) * Vector3.Normalize(contact.normal) - velocity; //Following formula  v' = 2 * (v . n) * n - v
 
         velocity *= -1;// Dont know why I had to multiply by -1 but it's inversed otherwisae
     }
 }

I hope this reply become useful to other users.

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

99 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

Related Questions

2D collision trouble, character falling through map 1 Answer

Rotate camera on collision of character 1 Answer

I have two RaycastHit2Ds but only one is activating 0 Answers

OnTriggerExit not working 0 Answers

Improve ParticleSystem physic 0 Answers


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