- Home /
Question by
ReginTheSmith · May 12 at 04:56 PM ·
rotationaiquaternionplanetspherical
How to get an enemy to face me on a spherical word
Recently ive been making a solar system space game with spherical planets and decided I wanted to add enemies,
so after looking online and some trial and error got this script ```
public float walkSpeed = 8;
public Rigidbody rb;
public Transform target;
Vector3 moveAmount;
float dist;
public float visionRadius = 10;
// Update is called once per frame
void Update()
{
dist = Vector3.Distance(transform.position, target.position);
if (dist <= visionRadius)
{
Vector3 movementDir = (target.transform.position - transform.position).normalized;
moveAmount = movementDir * walkSpeed;
}
}
void Move()
{
if (dist <= visionRadius)
{
rb.MovePosition(transform.position + moveAmount * Time.fixedDeltaTime);
FaceTarget();
}
}
private void FixedUpdate()
{
Move();
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.localRotation = lookRotation;
}
```
which gets it to follow the player but I was wondering how to get it to face the player at the same time
the face target function and the built-in lookat function came with some weird results
does anyone know what the math would be to get it to face me?
screen-shot-2022-05-12-at-103328-am.png
(105.3 kB)
screen-shot-2022-05-12-at-105443-am.png
(78.5 kB)
Comment
Your answer
Follow this Question
Related Questions
Boids for 2D 0 Answers
How can you tell if a gameobject is looking directly at another gameobject? 1 Answer
getting AI to look Left, then Right, then resume path 1 Answer
Planetoid Gravity 1 Answer
Basic AI Locked Axis 1 Answer