I have a “cannon” (cylinder) with a pivot point gameobject set at the tip of it. I use code to rotate the pivot point, in turn, rotating the cylinder.
However, as the cylinder moves left, right, up, and down, the cylinder will roll as I move the cannon. My question is, how can I keep the cylinder from rolling as I move it? Or even to compensate for the rolling as the cannon moves.
//When the gun is in use
private void Active() {
if(Input.GetKey(KeyCode.D)) {
pivot.transform.Rotate(new Vector3(0, 0, -1), 1 * gunSensitivity * Time.deltaTime);
}
if(Input.GetKey(KeyCode.A)) {
pivot.transform.Rotate(new Vector3(0, 0, 1), 1 * gunSensitivity * Time.deltaTime);
}
if(Input.GetKey(KeyCode.W)) {
pivot.transform.Rotate(new Vector3(1, 0, 0), 1 * gunSensitivity * Time.deltaTime);
}
if(Input.GetKey(KeyCode.S)) {
pivot.transform.Rotate(new Vector3(-1, 0, 0), 1 * gunSensitivity * Time.deltaTime);
}
//Shooting
if(Input.GetKeyDown(KeyCode.Space) && canShoot == true) {
GameObject clone;
clone = (GameObject)Instantiate(shellPrefab, shellLauncher.transform.position, pivot.transform.rotation);
clone.rigidbody.AddRelativeForce(new Vector3(0, -1, 0) * shellSpeed);
canShoot = false;
reloadingActive = true;
}
if(gunActive == false) {
_state = State.Returning;
}
//Keeps gun face up
}
//When you have left the gun and it is returning to it's original position
private void Returning() {
gunnerPanel.tag = "Untagged";
if(endOfBarrel.transform.position != returnPoint.transform.position) {
pivot.transform.rotation = Quaternion.Slerp(pivot.transform.rotation, originalRotation, Time.deltaTime * resetSensitivity);
//Debug.Log("Not Equal");
}
else {
Destroy(returnPoint);
_state = State.Inactive;
//Debug.Log("Equal");
}
}