Hey, I’m playing about with controls for orbiting a fixed point, so for example I have a sphere which is my target as a sort of anchor point at 0,0,0 and a cube at 0,0 -10, what I’m wanting to to is when using WASD the cube can orbit the sphere 360 degrees and always remaining the same distance away. The problem I’m having is that my up down controls invert based on the positive or negative position of the cube, so at -Z the cube will go up and down but at +Z they switch to down up and the speed also changes based on the z position at +Z the speed goes really slow and at -Z the speed moves as set. I’m also using a more basic set of controls with if statements as I’m still getting used to unity and its also been quite a while since I last used c#.
My Code
using UnityEngine;
using System.Collections;
public class Cube: MonoBehaviour
{
public GameObject target = null;
public bool OrbitY = false; //Up/Down
public bool OrbitX = false; //Left/Right
public float speed = 0.5F;
void Update ()
{
if (target != null)
{
transform.LookAt(target.transform);
if (Input.GetKey(KeyCode.W) && OrbitY)
{
transform.RotateAround(target.transform.position, Vector3.left, speed);
}
if (Input.GetKey(KeyCode.A) && OrbitX)
{
transform.RotateAround(target.transform.position, Vector3.up, speed);
}
if (Input.GetKey(KeyCode.S) && OrbitY)
{
transform.RotateAround(target.transform.position, Vector3.right, speed);
}
if (Input.GetKey(KeyCode.D) && OrbitX)
{
transform.RotateAround(target.transform.position, Vector3.down, speed);
}
}
}
}