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 Karsnen_2 · Apr 05, 2014 at 05:14 PM · c#rotationquaternionprojectilesimple

Rotate a projectile on Z axis for a 2D Game played on X and Y axis?

Hello,

The game always stays at z = 0 and movement of a projectile (non-kinematic rigidbody collider) is always on X and Y axis. Now when the projectile hit a wall (static collider) - it reflects.

I have got the reflection right, but can't figure a way to turn the projectile in the way of its movement. I want to keep angles on X and Y to be zero and just change the angles on Z axis.

It is one of those questions which, you know, it is very simple - hence I am missing something simple. I wish if you guys could help to me get it.

 // Code for projectile
 
 private void OnCollisionEnter(Collision hit)
     {
         if(hit.transform.tag == "SideCollider")
         {
             dirToTravel =  Vector3.Reflect(dirToTravel, hit.contacts[0].normal);

                     // The below line is not right
             this.transform.rotation = Quaternion.LookRotation(Vector3.Reflect(this.transform.forward, dirToTravel));        
             }
         }


IMAGES:

PROJECTILE properties

alt text

SIDE COLLIDER properties

alt text

Thank you.

regards, Karsnen

EDIT : The projectile is a non-kinematic rigidbody collider. I changed it now and thanks for @pako to notifying me that.

sidecollider.png (27.8 kB)
projectile.png (33.0 kB)
Comment
Add comment · Show 5
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 robertbu · Apr 05, 2014 at 06:41 PM 0
Share

As a first try, add the optional second parameter to your Quaternion.LookRotation(). Use Vector3.forward. That does not fix the problem in all situations, but it works for most.

avatar image Karsnen_2 · Apr 05, 2014 at 06:49 PM 0
Share

I have this

 this.transform.rotation = Quaternion.LookRotation(Vector3.Reflect(this.transform.forward, dirToTravel), Vector3.forward); 


It did not work. I debugged the ray for dirToTravel. It is the exact direction of travel. I just want to rotate my project in that vector. THats all. $$anonymous$$an I am missing something here.

avatar image sumeetkhobare · Apr 05, 2014 at 07:00 PM 0
Share

Why dont you try to freeze the projectile's X and Z rotation. and let Y be free.. But I am not sure..

if you get the reflection right..I will tell you one thing..I assu$$anonymous$$g basic maths..try this.

 transform.rotation = Quaternion.LookAt(hit.contacts[0].point+dirToTravel);

I am assu$$anonymous$$g that the 'transform' is at 'hit.contacts[0].point'

Tell me if it works.

avatar image pako · Apr 05, 2014 at 07:39 PM 0
Share

You say in your question that you use a "kinematic rigidbody collider", but the Inspector for the projectile does not have Rigidbody.Is$$anonymous$$inematic checked. So, maybe the physics engine adds some force to the projectile during collision that affects the resulting angle of travel(away from reflection angle). Have you tried your code with Is$$anonymous$$inematic checked?

avatar image Karsnen_2 · Apr 06, 2014 at 12:22 AM 0
Share

@pako :Pardon me. you were right. But i have to keep it as non-kinematic collider. $$anonymous$$y fault in the question and I have changed it.

@summetkhobare Yes it is basic math. I just don get the answer. But anyway, I have locked rotation along X and Y as the project can only rotate on Z axis. and that line of code did not work. It rotates beyond Z axis even though I applied constraints on the inspector. The Vector3 dirToTravel is a normalized variable. Does that have any effect?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ivomarel · Apr 05, 2014 at 06:53 PM

Check out these posts:

http://answers.unity3d.com/questions/59751/reflection-angle-change-objects-rotation.html

http://answers.unity3d.com/questions/52751/reflect-a-angle-for-a-rotation.html

They were only a search away. Hope it helps.

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 Karsnen_2 · Apr 06, 2014 at 12:16 AM 0
Share

Yeah I tried those as they were a search away but ivomarel I got this

http://pastebin.com/jcsFmABR

An error. Wierd. I think I went with the rules of params in the methods but still nope.

avatar image ivomarel · Apr 08, 2014 at 06:53 PM 0
Share

Quaternion qNewRotation = Quaternion.SetFromToRotation(hit.contacts[0].normal, v3NewDirection);

SetFromToRotation is a public method, not public static. So it should be like qNewRotation.SetFromToRotation(...)

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

22 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Object Local Y-Up being forced to be World Y-Up 1 Answer

How to Change rotation while preserving local horizontal rotation 1 Answer

better way to rotate instead Coroutine 1 Answer

(c#) NPC rotator 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