I am pretty new to unity, and am trying to convert this script from 3d to 2d. I need to have it circle an object solely on the x,y axis. Any suggestions?
Here is my code:
using UnityEngine;
using System.Collections;
public class orbit : MonoBehaviour {
// Use this for initialization
void Start () {
}
public Transform target;
void Update () {
Vector3 relativePos = (target.position + new Vector3(0, 1.5f, 0)) - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
Quaternion current = transform.localRotation;
transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime);
transform.Translate(0, 0, 3 * Time.deltaTime);
}
I’m not sure if I understand this line of you correctly ‘circle solely on x,y axis’. But if I do then this piece of code should make it rotate in the same way as your current code, except that only x and y position change, z stays the same. I used this forum post (the answer from yuriythebest) to make this.
Quaternion newRotation = Quaternion.LookRotation(transform.position - target.position, Vector3.forward);
newRotation.x = 0;
newRotation.y = 0;
newRotation *= Quaternion.Euler(-90, 0, 90); // Rotate to make the forward instead of he up face our target
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotateSpeed);
transform.Translate(0, 0, speed * Time.deltaTime);