Hi
i have problem with my grenade as they after Instantiate just move to wrong place. Every thing works fine. Grenade are being thrown right and move as intended after being thrown but they first teleport to wrong position relative to character who throws it. I search everywhere but didn’t find anything. heres the scripts
void FireGrenade()
{
if (Random.value < fireGrenade && Vector3.Angle(bulletSpawn.forward, targetLastKnownPos- bulletSpawn.position) < maxGrenadeFireAngle && grenadeObject)
{
float dist = Vector3.Distance(targetLastKnownPos, bulletSpawn.position);
if (dist < maxDistForFire && dist > minDistForFire)
{
StartCoroutine(SetTimeUntilNextGrenade());
GameObject currentGrenade = (GameObject)(Instantiate(grenadeObject, bulletSpawn.position, bulletSpawn.rotation));
currentGrenade.transform.LookAt(targetLastKnownPos);
if (currentGrenade.GetComponent<AdvanceAI.Grenade>())
{
currentGrenade.GetComponent<AdvanceAI.GrenadeScript>().SetTarget(targetLastKnownPos);
}
}
}
}
public void SetTarget(Vector3 pos)
{
target = pos;
hasTarget = true;
ActualGrenade();
}
void ActualGrenade()
{
float throwForce = 0;
if (hasTarget)
{
float xDist = Vector2.Distance(new Vector2(transform.position.x, transform.position.z), new Vector2(target.x, target.z));
float yDist = -(transform.position.y - target.y);
//Calculate force required
throwForce = xDist / (Mathf.Sqrt(Mathf.Abs((yDist - xDist) / (0.5f * (-Physics.gravity.y)))));
throwForce = 1.414f * throwForce / Time.fixedDeltaTime * GetComponent<Rigidbody>().mass;
}
myRigidBody = GetComponent<Rigidbody>();
myRigidBody.AddRelativeForce(Vector3.forward * throwForce);
//Start the time on the grenade
StartCoroutine(StartDetonationTimer());
}