- Home /
GameObject get launched underneath the terrain when using AddForce
According to my script the car should be launched into the air and when it lands back on the ground it should resume its original track of the navmesh. Sometimes when another gameobject collides with this gameobject(car) the car gets launched into "the ground"( below the terrain (y = -223.32 to be exact) and then appears again and follows its orignal track of the navmesh.
It is also worth noting this only happens on devices with a high fps like a pc or high-end android devices. On my phone (lower fps) everything works perfectly.
Rigidbody rb;
NavMeshAgent agent;
void OnCollisionEnter (Collision other)
{
if(other.gameObject.CompareTag("Wrecking Ball") && time >= 1.0f)
{
time = 0;
InvokeRepeating ("Timer", 1.0f, 0.5f);
//pause navmesh oncollision
agent.updatePosition = false;
agent.Stop ();
//effect of collision
health = health - damage;
rb.AddForce (new Vector3 (0, 15, 0), ForceMode.Impulse);
transform.Rotate (35, 0, 0);
//resume after 2 seconds
InvokeRepeating("Resume",2.5f,0.1f);
}
void Resume()
{
//resume original track of car
if (gameObject.transform.position.y <= 0.501 && health != 0)
{
agent.Warp (transform.position);
//if using infinite scene another script is called for setting the correct track of the car
if (SceneManager.GetActiveScene().name == "Infinite")
{
InfiniteAgent infiniteAgent = gameObject.GetComponent<InfiniteAgent> ();
infiniteAgent.destPoint -= 1;
}
else
{
AgentScript agentScript = gameObject.GetComponent<AgentScript> ();
agentScript.destPoint -= 1;
}
agent.Resume ();
agent.updatePosition = true;
CancelInvoke ();
}
}
void Timer()
{
//so the wrecking ball isn't destroying the car right away
time += 1;
}
Your answer
Follow this Question
Related Questions
Add force to an arrow 1 Answer
Object doesn't stop moving OnCollisionEnter 0 Answers
Drag a ball to add force to a ball by touch screen 1 Answer
Rigidbody not calling OnCollisionEnter 1 Answer
Nav Mesh Agent don't let me jump 2 Answers