Trying to switch two moving character's positions. Only working sometimes.
So, I'm making some characters with abilities and this one has a decoy which is sent walking forward. I then can press the same button to switch position with it but the problem is, it doesn't always work. Sometimes It's only my character who's moved to the decoy position and other times It's only the decoy who's moved to the player. There are also times where both are correctly moved.
The decoy walks forward and after a specified time it searches in a range for enemies, if anyone crosses the range the decoy then follows the enemy as a NavMesh agent.
I've tried so many things but nothing seems to work out. It doesn't always work and I can't seem to understand why.
Here's the decoy Update:
private void Update()
{
lastPosition = transform.position;
timeElapsed += Time.deltaTime;
if (timeElapsed <= secondsForTarget)
{
PlayRunningAnimation(true);
agent.Move(transform.forward * movementSpeed * Time.deltaTime);
}
else
{
if (!enemyFound)
{
PlayRunningAnimation(false);
GameObject tempEnemy = null;
foreach (GameObject enemy in enemiesList)
{
if (Vector3.Distance(transform.position, enemy.transform.position) <= targetRadius)
{
if (nearestEnemy == null) nearestEnemy = enemy;
tempEnemy = enemy;
enemyFound = true;
}
if (nearestEnemy != null)
if (Vector3.Distance(transform.position, tempEnemy.transform.position)
< Vector3.Distance(transform.position, nearestEnemy.transform.position))
nearestEnemy = tempEnemy;
}
}
else
{
PlayRunningAnimation(true);
agent.SetDestination(nearestEnemy.transform.position);
}
}
if (nearestEnemy != null &&
Vector3.Distance(transform.position, nearestEnemy.transform.position) <= agent.stoppingDistance)
PlayRunningAnimation(false);
if (timeElapsed >= decoyLifetime)
DestroyDecoy(vanishEffect);
}
And here is the player's ability which runs inside its own Update when the button is pressed:
protected override void MovementAbility()
{
if (!attackFlagMA)
{
decoy = Instantiate(decoyMA, transform.position, transform.rotation).GetComponent<DecoyController>();
decoyCollider = decoy.GetComponent<CapsuleCollider>();
decoy.Initialize(PlayerNumber, decoyLifetime, maximumHealth, charMovement.MovementSpeed, targetRadius, explosionRadius, explosionDamage, secondsUntilSeekingTargetMA);
attackFlagMA = true;
}
timeElapsedMA += Time.deltaTime;
if (decoy != null)
if (Vector3.Distance(transform.position, decoy.transform.position) > decoyCollider.radius * 2)
decoyCollider.enabled = true;
if (decoy != null)
if (InputManager.GetButtonDown(PlayerNumber, "MA") && !positionSwitched && timeElapsedMA > timeForSwitch)
{
positionSwitched = true;
Vector3 tempPos = decoy.transform.position;
Quaternion tempRot = decoy.transform.rotation;
Instantiate(switchPositionEffect, transform.position, transform.rotation);
Instantiate(switchPositionEffect, decoy.transform.position, decoy.transform.rotation);
decoy.transform.position = transform.position;
transform.position = tempPos;
decoy.transform.rotation = transform.rotation;
transform.rotation = tempRot;
}
if (timeElapsedMA >= decoyLifetime)
{
movementAbility = false;
attackFlagMA = false;
positionSwitched = false;
timeElapsedMA = 0;
}
}
Your answer
Follow this Question
Related Questions
how to move the enemy in the current position of the player but only ones? 0 Answers
Camera isn't move position? Why my camera isn't change position? 0 Answers
I'm making a simple platformer and I want the position to reset when you fall off the screen 2 Answers
Position of empty game objects 1 Answer
Different jump height 1 Answer