- Home /
Bounce rigidbody backwards without knowing its rotation
col.GetComponent<PlayerRigidbodyRedirector>()._Rigidbody.AddExplosionForce(_ParentScript._PushBack, transform.InverseTransformPoint(col.transform.position), Mathf.Infinity, 1f, ForceMode.Impulse);
I have tried everything I could think of, I have a cube with a trigger that acts as a "killer laser" thing and I want it to shoot back the player when he touches it.
There's only one issue though, I don't have access to the player's rotation (it's always 0,0,0), this means I need to somehow find a way to position the explosion spawn point in front of the player without knowing where forward is.
Does anyone know how I can do that? It always only works in one direction...
I want to add that if I make it just transform.position
, the origin of the laser. wHEN the player touches anything but the centre point, he will be shot at an angle.
Of course, I could tell it to use a vector 3 with all being the transform position and then the x axis be the player's own x axis, but then what happens when I rotate it and the X axis becomes the Z axis?
Answer by HugoCortell_VFS · Aug 09, 2020 at 07:31 PM
I found out how to do it but please if you know a more elegant way, let me know!
// CHECK ANGLE AND EXECUTE PUSHBACK BASED ON RESULTS.
if (_ParentScript.gameObject.transform.eulerAngles.y == 0)
{col.GetComponent<PlayerRigidbodyRedirector>()._Rigidbody.AddExplosionForce(_ParentScript._PushBack, new Vector3(transform.position.x, transform.position.y, col.transform.position.z), Mathf.Infinity, 1f, ForceMode.Impulse);}
if (_ParentScript.gameObject.transform.eulerAngles.y == 90)
{col.GetComponent<PlayerRigidbodyRedirector>()._Rigidbody.AddExplosionForce(_ParentScript._PushBack, new Vector3(col.transform.position.x, transform.position.y, transform.position.z), Mathf.Infinity, 1f, ForceMode.Impulse);}
if (_ParentScript.gameObject.transform.eulerAngles.z == 90) // NOW ITS UP-N-DOWN
{col.GetComponent<PlayerRigidbodyRedirector>()._Rigidbody.AddExplosionForce(_ParentScript._PushBack, new Vector3(transform.position.x, col.transform.position.y, transform.position.z), Mathf.Infinity, 1f, ForceMode.Impulse);}
This code makes me doubt myself.
Your answer
Follow this Question
Related Questions
RigidBody 2D x velocity function 1 Answer
Is there a way to have very large mass 0 Answers
How to calculate desire angular velocity? 1 Answer
Calculating force 1 Answer