- Home /
Clamped rotation of turret problem
Hello,
I'm working on a game where the player controls a spaceship, with turrets. I'd like these turrets to have limited rotation, so it's not possible to rotate the turrets to where they'd clip through the ship or other such things.
The ship is able to move freely in 3d, and I need the turrets to be limited in relation to the position of the ship, not the world. I've been able to clamp them just fine with the world, but not in respect to the ship.
Strangely, this sort of works right now for one side of the rotation, but not the other. Right now the turrets can turn left 90 degrees, but that limit doesn't rotate with the ship. However, it currently can't rotate at all to the right, and that limit does move with the ship.
Here's the code for the turrets rotation:
using UnityEngine;
using System.Collections;
public class TurretBaseController : MonoBehaviour {
//public GameObject player;
public Transform projSpawn;
public GameObject projectile;
public float sensY = 300f;
public float sensX = 300f;
public float minX = 0f;
public float maxX = 0f;
float rotationX = 0f;
public float rotSmooth;
public float offSetX;
public float offSetY;
public float offSetZ;
public GameObject Player;
public Quaternion rotation;
// Update is called once per frame
void Update()
{
minX = Player.transform.rotation.y - 360f;
maxX = Player.transform.rotation.y + 360f;
//axis the object can rotate along
rotationX += Input.GetAxis("Mouse X") * sensX * Time.deltaTime;
Vector3 rotation = new Vector3(0, Mathf.Clamp (rotationX, -90f, 90f), offSetZ);
transform.eulerAngles = rotation;
float x = transform.localEulerAngles.y;
x = Mathf.Clamp(x, -180f, 180f);
// clamp local.
transform.localEulerAngles = new Vector3(0, x);
}
}
The whole thing has me stumped. Any help would be greatly appreciated.
Your answer
Follow this Question
Related Questions
How to clamp rotation of an object? 2 Answers
How to use mathf.clamp? 2 Answers
How to Clamp a Quaternion Camera Rotation? 0 Answers
Bizzare problem with limiting camera veritcal rotation 2 Answers
Clamping Vector3 2 Answers