I’ve been stuck on this for 6 days now… the problem, essentialy, is that I don’t want it to stop, but to move a little bit after that due to momentum. The idea is this: use the right joystick to detect a target angle and rotate around this.gameobject until you reach that angle. It works fine, it even does what is supposed to when you release the joystick, but is a different story when you hold it. My theory is that it reaches the desired position and stops changing velocity (my current approach, I already tried with setting positions and translate and physics materials). Once it stops, it keeps going due to momentum, but after the treshold becomes too big, this part of the code reactivates, moving it bacwards since the target angle never changed. And I don’t know how to fix this, so please, help me. I don’t want to spend other 6 days on this…
Vector2 _linealMovement = new Vector2(itsMoveXZ[i].x, itsMoveXZ[i].z);
float linealMovement_mag = _linealMovement.magnitude;
float targetAngle = angleDeg - 90;
if (targetAngle < 0) targetAngle += 360; //between 0 and 360
Vector3 rotatedDirection = allies[i].transform.position;
rotatedDirection.x = rotationRadius[i] * Mathf.Cos(-targetAngle * Mathf.Deg2Rad);
rotatedDirection.z = rotationRadius[i] * Mathf.Sin(-targetAngle * Mathf.Deg2Rad);
Vector3 endPosition = new Vector3 (this.transform.position.x + rotatedDirection.x, rotatedDirection.y, this.transform.position.z + rotatedDirection.z);
Vector3 startDirection = allies[i].transform.position - this.transform.position;
startDirection.y = 0;
Vector3 endDirection = new Vector3(rotatedDirection.x, startDirection.y, rotatedDirection.z);
float radius = startDirection.magnitude;
startDirection.Normalize();
endDirection.Normalize();
Vector3 initial = Vector3.right;
float initialToActualAngle = Vector3.SignedAngle(initial, startDirection, Vector3.up);
if (initialToActualAngle < 0) initialToActualAngle += 360f; //between 0 and 360
float startEndAngle = Vector3.SignedAngle(startDirection, endDirection, Vector3.up); //-180 and 180
Vector3 mid_rotatedPosition = allies[i].transform.position;
float totalCircleLength = Mathf.PI * (radius * 2);
linealMovement_mag;
float PerimeterScalar = Mathf.Abs(startEndAngle) / 360f;
float maxCircularMovement = totalCircleLength * PerimeterScalar;
float furtherFix = LogisticFunction(ref radius);// * 7 / 32 - 3 / 16;
float speedScalar = linealMovement_mag * Time.deltaTime * furtherFix / maxCircularMovement;
if (speedScalar > 1) speedScalar = 1;
speedAngle[i] = initialToActualAngle + startEndAngle * speedScalar;
if (speedAngle[i] > 180) speedAngle[i] -= 360; // -180 and 180
mid_rotatedPosition.x = this.transform.position.x + rotationRadius[i] * Mathf.Cos(-speedAngle[i] * Mathf.Deg2Rad);
mid_rotatedPosition.z = this.transform.position.z + rotationRadius[i] * Mathf.Sin(-speedAngle[i] * Mathf.Deg2Rad);
Vector3 newPosition = mid_rotatedPosition;
//allies[i].transform.position = newPosition; essentialy this, but with physics, (originally, now it's not even the actual newPosition
bool staticAngle = false;
if (Mathf.Abs(targetAngle - prevTargetAngle) <= 0.75f) //THIS IS THE PROBLEM
{
staticAngle = true;
}
if (staticAngle)//Mathf.Abs(startEndAngle) >= 0.75f)// || staticAngle)
{
//this scope apparently is repeated constantly back and forth
print("startEnd: " + startEndAngle);
itsMoveXZ[i] = (newPosition - allies[i].transform.position).normalized * allySpeed[i];
bool allyWalksOnIce = false;
Vector3 prev_allyVelocity = new Vector3(ally_RB[i].velocity.x, 0, ally_RB[i].velocity.z);
float allyMagDiff = itsMoveXZ[i].magnitude - prev_allyVelocity.magnitude;
Vector3 newAllyVelocity = ally_RB[i].velocity;
newAllyVelocity.x = itsMoveXZ[i].x;
newAllyVelocity.z = itsMoveXZ[i].z;
if (itsMoveXZ[i] != Vector3.zero && allyMagDiff >= -0.75f)
{
/*float dot = Vector3.Dot(ally_RB[i].velocity, newAllyVelocity);
float eq = dot / (ally_RB[i].velocity.magnitude * newAllyVelocity.magnitude);
float aCos = Mathf.Acos(eq);
print("acos: " + aCos);*/ //I also tried to compare and check if the movement was opposite, but same problem
ally_RB[i].velocity = newAllyVelocity;
}
else if (!allyWalksOnIce)
{
Vector3 prevAllyVel = ally_RB[i].velocity;
prevAllyVel.x *= slideLimiter;
prevAllyVel.z *= slideLimiter;
ally_RB[i].velocity = prevAllyVel;
}
}