- Home /
Question by
Roritamashi · Dec 15, 2018 at 03:08 AM ·
rigidbodyraycastquaternion
Wrong direction of a object when use raycast.normal
I'm trying to make a bow, which throws arrows from an object and these go to the center where it is pointing (crosshair) but the created object has incorrect rotations.
it happends when the arrow hit the walls or another objects and get stuck in it, but it have wrong rotations and if i create only the arrow from the emitter, the arrow never touchs the screen center. but if i put the calculations to rotate the arrow to the screen center the rotation seems to go wrong here is a video https://youtu.be/FlXLME10iQ8
void ArrowShoot()
{
Ray ray = FpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Vector3 colisionPoint = hit.point;
Vector3 ArrowVector = colisionPoint - ArrowEmitter.transform.position;
GameObject ArrowCopy = Instantiate (ArrowPrefab, ArrowEmitter.transform.position, ArrowEmitter.transform.rotation);
Quaternion rotate = Quaternion.FromToRotation (Vector3.forward, hit.normal);
ArrowCopy.transform.rotation *= rotate;
Rigidbody ArrowRigidBody = ArrowCopy.GetComponent<Rigidbody>();
Vector3 v3Force = ArrowForze*(FpsCam.transform.forward + ArrowVector);
ArrowRigidBody.AddForce(v3Force);
().AddForce(ArrowVector * ArrowForze);
}
else
{
Vector3 v3Direction = ArrowForze * FpsCam.transform.forward;
GameObject ArrowCopy = Instantiate (ArrowPrefab, ArrowEmitter.transform.position, ArrowEmitter.transform.rotation);
Rigidbody ArrowRigidBody = ArrowCopy.GetComponent<Rigidbody>();
ArrowRigidBody.AddForce(v3Direction*10);
}
Comment