I'm trying to predict the position of a game object using Physics.autoSimulation (the game object has a rigidbody attached). The problem is that the object has some kind of flickering when I trigger the simulation. Like this:
I read this thread. I'm starting to think that I'm not using autoSimulation correctly. Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestBall : MonoBehaviour {
public float timeCheck = 3f;
public float force = 10f;
private Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody>();
}//end Start
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
//Starts the simulation...
Physics.autoSimulation = false;
rb.AddForce(Vector3.right * force, ForceMode.Impulse);
Vector3 futurePosition = CheckPosition(rb);
print("Future position at " + futurePosition);
//...and move the GameObject for real
rb.AddForce(Vector3.right * force, ForceMode.Impulse);
}//end if
}//end Update
Vector3 CheckPosition (Rigidbody defaultRb) {
Vector3 defaultPos = defaultRb.position;
Quaternion defaultRot = defaultRb.rotation;
float timeInSec = timeCheck;
while (timeInSec >= Time.fixedDeltaTime) {
timeInSec -= Time.fixedDeltaTime;
Physics.Simulate(Time.fixedDeltaTime);
}//end while
Vector3 futurePos = defaultRb.position;
Physics.autoSimulation = true;
defaultRb.velocity = Vector3.zero;
defaultRb.position = defaultPos;
defaultRb.rotation = defaultRot;
return futurePos;
}//end CheckPosition
}//end TestBall
It is like for a tiny moment, the game object moves at the end of the simulation and then comes back because the positions resets in CheckPosition
function.
Thanks for the help!