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 /
This question was closed Apr 24, 2018 at 10:59 AM by polan31 for the following reason:

Other

avatar image
0
Question by polan31 · Apr 02, 2018 at 11:08 PM · rotationdirectionmovingbounces

How to make the bullet rotation (by 90 degrees) in the direction of movement?

I have 2D bullets in a simple simulation (with Circle Collider 2D).

It is based on a simple script, that I join.

How to make the bullet bounce off the wall change the rotation by 90 degrees in the direction of movement?

The first and third picture shows how the bullet should move.

The fourth picture shows how the bullet moves.

Could someone help and explain?

Script:

 private Rigidbody2D rb;

 float constantSpeed = 10f;

 // Use this for initialization

 void Start () {

     rb = GetComponent<Rigidbody2D> ();

     rb.velocity = new Vector2 (6f, 6f);

 }

 // Update is called once per frame

 void Update() {

     rb.velocity = constantSpeed * (rb.velocity.normalized);
 }



alt text

alt text

pic-1.png (18.3 kB)
pic-3.png (18.1 kB)
Comment
Add comment · Show 1
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 polan31 · Apr 02, 2018 at 11:10 PM 0
Share

I'm sorry, I forgot the fourth picture.

alt text

pic-4.png (20.7 kB)

4 Replies

  • Sort: 
avatar image
3

Answer by oroora6_unity · Apr 03, 2018 at 05:06 PM

If you're using unity physics your rigidbody should have a velocity, which is a Vector3 which is the distance from a point which in our case is the center of the world (0,0,0). This makes it possible to get a rotation from a Vector by calculating the direction of the distance, luckly for us the vector3 defines that already and unity has a built in method in the Quaternion class to calculate it already. so all you need to do is:

     //Play this when the direction of your bullet changes, so after a collision
     Rigidbody2D rb = GetComponent<Rigidbody2D>();
     transform.rotation = Quaternion.LookRotation(rb.velocity);

Hope it helps!

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 Cornelis-de-Jager · Apr 03, 2018 at 02:04 AM

Try this:

 void Update() {
  rb.velocity = constantSpeed * (rb.velocity.normalized);

   // Make it look in direction of travel
   transform.LookAt ( transform.Position + rb.velocity );
 }
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 polan31 · Apr 03, 2018 at 08:53 AM 0
Share

Thanks for your answer, but there is an error after adding script:

error CS0121: The call is ambiguous between the following methods or properties: UnityEngine.Vector3.operator +(UnityEngine.Vector3, UnityEngine.Vector3)' and UnityEngine.Vector2.operator +(UnityEngine.Vector2, UnityEngine.Vector2)'

avatar image
0

Answer by ngoson107 · Apr 03, 2018 at 02:11 AM

You can create physic material 2d, then change index bounce of physic material 2d. Then add physic material 2d to material of Circle Collider.

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 polan31 · Apr 03, 2018 at 08:58 AM 0
Share

Thank you for your answer.

You probably didn't notice, but I wrote that the bullet is bouncing.

This is possible because it contains physic material 2d.

The problem is not bounce, but no rotation in the direction of movement.

But anyway, I'm grateful for the answer :)

avatar image
0

Answer by meat5000 · Apr 03, 2018 at 10:23 PM

On Collision, Find the ContactPoint2D.normal and use it to find the angle difference between the projectile and normal. Use it to work out how much rotation your bullet needs.

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

Follow this Question

Answers Answers and Comments

112 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 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

make object move in the same direction it faceing 1 Answer

how to tell a game object to move between 2 points(A,B) along x axis , right to left, and then move left to right back to its first position ??? 3 Answers

Set rotation based on 2 points problem 1 Answer

How can I get the rotated position without using Transform in 2D? 1 Answer

How to fix rotation while moving character 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