How to make an object orbit around another object?
The code I have at the moment is not working properly as it's not orbiting the right way around the object. its orbiting this way by going infront and then behind instead of in a circle around the character, any ideas on what to do to this code?;
using UnityEngine;
using System.Collections;
public class HoneyScript : MonoBehaviour {
GameObject Winnie;
public Transform center;
public Vector3 axis = Vector3.up;
public Vector3 desiredPosition;
public float radius = 2.0f;
public float radiusSpeed = 0.5f;
public float rotationSpeed = 80.0f;
// Use this for initialization
void Start () {
Winnie = GameObject.FindWithTag("Winnie");
center = Winnie.transform;
transform.position = (transform.position - center.position).normalized * radius + center.position;
radius = 2.0f;
}
// Update is called once per frame
void Update () {
transform.RotateAround(center.position, axis, rotationSpeed * Time.deltaTime);
desiredPosition = (transform.position - center.position).normalized * radius + center.position;
transform.position = Vector3.MoveTowards(transform.position, desiredPosition, Time.deltaTime * radiusSpeed);
}
}
game.jpg
(202.6 kB)
Comment
Your answer
Follow this Question
Related Questions
Why won't my 2D Sprite Move? 1 Answer
No overload for method. 1 Answer
Is it possible to add an Input Counter during Splash Screens? 0 Answers
crouch attack collider 2d question 1 Answer