- Home /
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
SIDE COLLIDER properties
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.
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.
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.
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.
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?
@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?
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.
Yeah I tried those as they were a search away but ivomarel I got this
An error. Wierd. I think I went with the rules of params in the methods but still nope.
Quaternion qNewRotation = Quaternion.SetFromToRotation(hit.contacts[0].normal, v3NewDirection);
SetFromToRotation is a public method, not public static. So it should be like qNewRotation.SetFromToRotation(...)