Hello everyone,
I’m trying a top down 2d shooter and have problems with the enemyTank.
I want to know when the rotation is complete so that the tank moves.
I thought of a Simpel if-Statement:
sry for bad English.
if(Rotation reached){Move Tank}
private void Patrol()
{
Rotate(_wayPoints[_randomSpot]);
transform.position = Vector2.MoveTowards
(transform.position, _wayPoints[_randomSpot].position, _speed * Time.deltaTime);
if (Vector2.Distance(transform.position, _wayPoints[_randomSpot].position) < 0.2f)
{
if (_waitTime <= 0)
{
_randomSpot = Random.Range(0, _wayPoints.Length);
_waitTime = _startWaitTime;
}
else
{
_waitTime -= Time.deltaTime;
}
}
}
private void Rotate(Transform _lookDirection)
{
Vector3 difference = _lookDirection.position - transform.position;
difference.Normalize();
float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler
(0f, 0f, angle + rotationOffset), 20 * Time.deltaTime);
}
public void RotateTowards()
{
float strength = 1f;
float str;
Vector3 direction = (transform.position - playerAgentBase.transform.position).normalized;
direction.y = 0;
str = Mathf.Min(strength * Time.deltaTime, 1);
Quaternion lookRotation = Quaternion.LookRotation(direction);
playerAgentBase.gameObject.transform.rotation = Quaternion.Slerp(playerAgentBase.gameObject.transform.rotation, lookRotation, str);
float angle = Quaternion.Angle(playerAgentBase.gameObject.transform.rotation, transform.rotation);
if (angle >=179f)
{
rotate = false;
}
}
void Update()
{
if (rotate)
{
RotateTowards();
}
}
Here’s how I approached this. I have an orientation
object as a child of my characters that represent their facing direction without actually rotating the game object in worldspace - when you see orientaiton
, that’s what it’s referencing. rotationSpeed
is just a float.
// Point character towards a rotation target.
void HandleFacingDirection() {
Quaternion targetDirection = GetTargetDirection();
orientation.rotation = Quaternion.Slerp(orientation.rotation, targetDirection, rotationSpeed * Time.deltaTime);
}
// Rotate character smoothly towards a particular orientation around the Z axis.
// Warning: I don't understand this math. If character rotation seems buggy, this is a
// potential culprit.
Quaternion GetTargetDirection() {
Vector3 target = orientTowards - transform.position;
float angle = Mathf.Atan2(target.y, target.x) * Mathf.Rad2Deg;
return Quaternion.AngleAxis(angle, Vector3.forward);
}
// used to calculate how far our facing direction is from our target facing direction.
// Useful for e.g. deciding if an enemy is facing a player enough for attacking to be a good idea.
public float GetAngleToTarget() {
return Quaternion.Angle(GetTargetDirection(), orientation.rotation);
}
If you use the GetAngleToTarget
function, you can check to see if the angle is less than a certain amount, and only move forward if it is. Float equality is usually a bad idea, so be wary of checking if it equals zero.