Hello,
I’m working on a project where I launch a ball with an AddForce, everything works well there.
Here is the script for that part :
void ThrowEntity()
{
GM.loadingRing.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
GM.rebondMax = (int)GM.powerRay / GM.rebondTimerDiviser;
GM.timerMax = (int)GM.powerRay / GM.rebondTimerDiviser;
if (GM.rebondMax < 1 || GM.timerMax < 1)
{
GM.rebondMax = GM.rebondMin;
GM.timerMax = GM.timerMin;
}
GM.rebondActual = GM.rebondMax;
GM.timerActual = GM.timerMax;
// Si on possède l'entité et qu'on relache le clic gauche, l'entité ne dépend plus du personnage et est propulsée vers l'endroit où l'on regarde à une force dépendant du temps d'appui du clic gauche
Ray rayon = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(rayon, out hitInfo))
{
GM.carrying = false;
GM.entity.transform.parent = null;
GM.entity.GetComponent<Rigidbody>().isKinematic = false;
GM.entity.GetComponent<Rigidbody>().AddForce(rayon.direction * GM.powerRay);
GM.trail.Play();
}
// On définit également la durée de vie de la trail en fonction de cette force
GM.trail_lifetime = GM.trailLifetimeMax * GM.powerRay / GM.powerThrowEntityMax;
GM.powerRay = 0;
}
The problem comes with a new mechanics I just added :
-
I slow mo the game and when I release the mouse click it change the direction by a new addForce. Here it works again.
There is the code :void SlowMotion() { if (Input.GetMouseButton(0)) { Ray rayon = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(rayon, out hitInfo)) { GM.entity.GetComponent<Rigidbody>().AddForce(rayon.direction * GM.ForceSlowMo, ForceMode.Impulse); } } Time.timeScale = GM.slowMoTimeScale; Time.fixedDeltaTime = Time.timeScale * .02f; }
The problem comes after. When I pick up the ball after using the slow motion, my rigidbody goes to sleep, so if I want to launch it again the addforce just doesn’t work because of that.
And the problem is, i can’t wake up this rigidbody for an unknown reason.
I’ve tried :
- WakeUp() : in update, at the start of the stack (to determine the force) and right before the launch.
- Tweaking the project settings and the treshold for sleeping rigidbody by putting it at 0
- doing a “entity.transform.position = entity.transform.position”
And noone of these solutions i’ve found on the net is working, i’m starting to being desperate.
Greetings and thanks for your help, also sorry for bad english.