- Home /
Controlling the rotation of nested objects
started messing with unity, making some kinda tank game as a test. But ive ran into an issue. I would like to know how to stop rotating the other two axes of the tank turret. At the moment, when you start moving side to side, while raising the cannon. It looks like the cannon still points forward, instead of staying static in the other axes. Ive tried looking around for references, but im not quiet sure i understand the whole concept of quaternions and euler angles just yet. Any help with this, will clear a lot of questions i have. I thought i had it when i was able to clamp the cannon to set a maximun height..but that lead me to other issues.
thanks!
Video link showing the problem.
here is my code
using UnityEngine;
using System.Collections;
public class cannonAttachment_1 : MonoBehaviour
{
public Rigidbody bullet;
public float raiseSpeed = 100f;
public float fireRate_1 = 10f;
public float pitchMaxValue = 1.0f;
float lockPos = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
float verticalMovement = Input.GetAxis("Vertical");
//Main Fire
if(Input.GetButtonDown("Fire1"))
{
Rigidbody newBullet = Instantiate(bullet,transform.position,bullet.rotation) as Rigidbody;
newBullet.AddForce(transform.forward*fireRate_1,ForceMode.VelocityChange);
}
if(verticalMovement !=0)
{
//raise the cannon up based on player feedback.
Vector3 finalDirection = new Vector3(0,verticalMovement,pitchMaxValue);
transform.rotation = Quaternion.RotateTowards(transform.rotation,Quaternion.LookRotation(finalDirection),Mathf.Deg2Rad*150.0f);
}
}
}
Take a look at Transform.localRotation. Your code on one local axis.
Your answer