I’m trying to rotate a gameobject while it’s being rotated around a pivot like a canon on a ship that’s circling a planet. the canon shouldn’t be able to rotate all the way around though, so i’ve tried using mathf.clamp to limit it but what happens is that it stops facing the center when it rotates. i feel like this is a mathematical problem where i have to calculate and take into account the movement of the ship around the pivot but my brain just isn’t making the connection. Any help?
using UnityEngine;
using System.Collections;
public class NewBehaviourScript1 : MonoBehaviour {
public float horizRotation = 0.0f;
void Update () {
transform.RotateAround (Vector3.zero, Vector3.back, 20 * Time.deltaTime);
transform.LookAt(Vector3.zero);
horizRotation += Input.GetAxis("Horizontal") *100* Time.deltaTime;
horizRotation = Mathf.Clamp(horizRotation, -45, 45);
transform.rotation = Quaternion.Euler(0, 0, horizRotation);
}
}
you should be able to drop the script onto a capsule that’s a bit above the centre of the scene and it will orbit 0,0,0. when you press the right or left arrow keys it should rotate the “cannon” but it does it relative to the vertical axis of the screen, not (0,0,0) like it should. Thanks in advance