I have a rocket that orbits a planet, but I am having a difficult time making the rocket turn as it moves. I have done research and looked through some unity docs, but haven’t been able to make it work. Here’s a video of my issue - - YouTube
Here’s my code:
using UnityEngine;
using System.Collections;
public class RocketOrbit : MonoBehaviour {
public Transform target;
//float rotSpeed=180f;
float speed = 2f;
float timeCounter = 0;
int orbitCounter = 0;
//private bool moving;
//private Rigidbody2D myRigidBody;
// Use this for initialization
void Start () {
//myRigidBody = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
RocketMove ();
//Attempt 1
/*var targetDir = target.position - transform.position;
var step = speed * Time.deltaTime;
var newDir = Vector3.RotateTowards (transform.forward, targetDir, step, 0.0f);
Debug.DrawRay (transform.position, newDir, Color.blue);
transform.rotation = Quaternion.LookRotation (newDir);*/
//Attempt 2
/*Quaternion rot = transform.rotation;
float z = rot.eulerAngles.z;
z -= Input.GetAxis ("Horizontal") * rotSpeed * Time.deltaTime;
rot = Quaternion.Euler (0, 0, z);
transform.rotation = rot;
Vector3 shipPos = transform.position;
Vector3 velocity = new Vector3 (0, Input.GetAxis ("Vertical") * speed * Time.deltaTime, 0);
shipPos += rot * velocity;
transform.position = shipPos;*/
if (orbitCounter == 5) {
Destroy (gameObject);
}
}
public void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Orbit Collider") {
orbitCounter++;
Debug.Log (orbitCounter);
}
}
public void RocketMove(){
//moving = true;
timeCounter += Time.deltaTime;
float x = Mathf.Cos (timeCounter)*2;
float y = Mathf.Sin (timeCounter)*2;
float z = 0;
transform.position = new Vector3 (x, y, z);
}
}