- Home /
Make An Enemy Rotate Around A Player
I am trying to have the flying eyeball enemy rotate around the player using this code for the rotation
v_diff = (target.position - transform.position);
atan2 = Mathf.Atan2(v_diff.y, v_diff.x);
transform.rotation = Quaternion.Euler(0f, 0f, atan2 * Mathf.Rad2Deg);
and the pivot of the sprite is where the pupil is though I have tried having it be the end of the tail to no avail. Please help me have the eye face the player.
Answer by Gruffy · Apr 08, 2016 at 11:44 AM
Hey GamerC4,
Although this will not just slot into your code as such , you can use this script to see the action of what you want and adapt it accordingly
Place script on the required rotating gameobject, nominate your target gameobject in the script inspector as the object to rotate around Use the arrow keys to test the rotation around your target object ...anyway below is the script, try this first and get back to here after and discuss how you want it developed into your existing scripts.
using UnityEngine;
using System.Collections;
public class RotateObjectAroundAPoint : MonoBehaviour
{
public GameObject goToRotate;
private Transform mTrans;
public GameObject target;
private Vector3 targetPosition;
public float speed;
private RotateObjectAroundAPoint inst;
void Start()
{
inst = this;
inst.mTrans = goToRotate.transform;
inst.targetPosition = target.transform.position;
inst.mTrans.LookAt(target.transform);
}
void Update()
{
//use arrow keys to mvoe object around clockwise or anticlockwise (additive speeps by holding one side or another for testing only)
if (Input.GetKey(KeyCode.LeftArrow))
{
speed++;
}
if (Input.GetKey(KeyCode.RightArrow))
{
speed--;
}
//this rotates this object around the nominated gameobject in the scene
inst.mTrans.RotateAround(targetPosition, Vector3.up, speed * Time.deltaTime);
}
}
Cheers for now, hope this start you on your journey :)
Answer by Ali-hatem · Apr 08, 2016 at 11:30 AM
i have a simple way eyeball script :
public float diameter;
public float RotateSpeed;
public Transform target;
void Update(){
transform.position = target.position; //assuming the eyeball should fallow the player.
transform.Translate(diameter,0,0);
transform.Rotate(0,0,RotateSpeed);
}