Answers for "Adjust custom PlayerControls to be used by enemies"
http://answers.unity.com/questions/1857522/adjust-custom-playercontrols-to-be-used-by-enemies.html
The latest answers for the question "Adjust custom PlayerControls to be used by enemies"Answer by basboogaard89
http://answers.unity.com/answers/1857527/view.html
To answer my own question, I guess the code that I tried earlier was indeed wrong, maybe code for 3D games, too much math, I don't know. The code below works for me, at least static forward movement:
if (target == null) {
// Get player input
h = -Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
} else {
// calculate input
// vector from this object towards the target location
Vector3 vectorToTarget = target.position - transform.position;
// rotate that vector by 90 degrees around the Z axis
Vector3 rotatedVectorToTarget = Quaternion.Euler(0, 0, 90) * vectorToTarget;
// get the rotation that points the Z axis forward, and the Y axis 90 degrees away from the target (resulting in the X axis facing the target)
Quaternion targetRotation = Quaternion.LookRotation(forward: Vector3.forward, upwards: rotatedVectorToTarget);
// Don't know why the previous code says 'facing the target' as it's 90 degrees too much
Vector3 euler = targetRotation.eulerAngles;
euler.z -= 90;
if (Vector2.Distance(transform.position, target.position) > 2)
{
h = Mathf.Clamp(euler.z - transform.rotation.eulerAngles.z, -1, 1);
v = 1;
}
}
// ... (apply force and rotation to RigidBody2D)Sat, 28 Aug 2021 09:39:11 GMTbasboogaard89