Hi guys,
I understand what is going on, but I don’t know why.
// This happens every frame in Unity to handle it's in built physics.. Here we find which Civilisation was the last target and set the other one as it.
void FixedUpdate()
{
// If the moon's just collided with an object, wait until it's timer's reset before re-targetting (Set in 'OnCollisionEnter').
if(hitDelay > 0)
{
hitDelay -= Time.deltaTime;
return;
}
moonAccel += Time.deltaTime * moonAccAmplify;
Vector3 currVel, playerPos;
float newSpeed = (moonAccel + moonDefaultForce);
//Sets the initial velocity for the lerp.
currVel = rigidbody.velocity;
// If player one set target as player two.
if(lastTarget == "PlayerOne")
{
playerPos = (GameObject.FindGameObjectWithTag("PlayerTwo").transform.position - transform.position).normalized;
rigidbody.velocity = Vector3.Lerp(currVel, playerPos * newSpeed, Time.deltaTime);
}
// If player two set target as player one.
if(lastTarget == "PlayerTwo")
{
playerPos = (GameObject.FindGameObjectWithTag("PlayerOne").transform.position - transform.position).normalized;
rigidbody.velocity = Vector3.Lerp(currVel, playerPos * newSpeed, Time.deltaTime);
}
}
public void Push()
{
RaycastHit hit;
Vector3 aimMoon = (moon.transform.position - transform.position).normalized;
Ray pushShotHit = new Ray(transform.position, aimMoon);
if(Physics.Raycast(pushShotHit, out hit, 10000))
{
if(hit.collider.tag == "Moon")
{
AttackMoon(hit, aimMoon);
}
}
}
void AttackMoon(RaycastHit hitMoon, Vector3 pushMoon)
{
hitMoon.rigidbody.AddForce(pushMoon * 10000, ForceMode.VelocityChange);
}
Now the Push function works absolutely great… when hitDelay is applied and the object is not using lerp. Does AddForce not over ride FixedUpdate code??? If it doesn’t is there a best practice to get around it?