- Home /
This question was
closed Aug 08, 2018 at 10:12 PM by
tormentoarmagedoom for the following reason:
The question is answered, right answer was accepted
Question by
Bokaii · Aug 08, 2018 at 07:24 PM ·
instantiateraycastfpsnormalize
Direction from two objects
Hi, I'm making an FPS and when I shoot I instantiate a bullet. I also cast a ray to get the direction I want to send the bullet. The problem I'm having is that when I hit something, it sends the bullet too high so it doesn't hit the target.
Here's the code:
//Aim Position
Vector3 aimPos;
//Cast ray
RaycastHit hit;
if(Physics.Raycast(mainCam.transform.position, mainCam.transform.forward, out hit, 50))
{
print(hit.transform.name);
aimPos = hit.point;
}
else
{
aimPos = mainCam.transform.position + mainCam.transform.forward * 50;
}
//Instantiate activeSpell.spellPrefab;
GameObject bullet= Instantiate(bulletPrefab, spawnPoint.position, Quaternion.identity);
//Addforce to spellPrefab
bullet.GetComponent<Rigidbody>().AddForce((aimPos - transform.position).normalized * Speed);
Is there a problem with my code? Or am I just doing something wrong?
Comment
Best Answer
Answer by tormentoarmagedoom · Aug 08, 2018 at 09:56 PM
Good day.
You are calculating the bullet direction from
transform.position
and i supose it should be
bullet.transform.position
Bye! >D