- Home /
How can i rotate both turret cannon and body same time or separate ?
I have in the Hierarchy a Turret and as a child also a Cannon. of the Turret. Then i have a Cube i'm using for testing to see if and when the turrer detecting it and rotating facing the cube.
The problem is that if i put the Cannon as child of the Turret and the script is on the Turret it will rotate with lag. But if i put the script only on the Cannon then the Cannon will rotate fine facing the Cube but the Turret body will not rotate.
I want to rotate both Turret body and Cannon together.
I recorded a small video showing the problem. First when the script is attached to the Cannon and then when the script is attached to the Turret.
And this is the script i'm using for rotating the turret: Either using the code it is now in Update or using the LookAt line same problem.
using UnityEngine;
using System.Collections;
public class Turret : MonoBehaviour
{
public GameObject target;
public float smooth = 1f;
public float rangeSqr = 30f;
private void Update()
{
float distanceSqr = (transform.position - target.transform.position).sqrMagnitude;
if (distanceSqr < rangeSqr)
{
//transform.LookAt(target.transform.position);
var targetRotation = Quaternion.LookRotation(target.transform.position - transform.position);
var str = Mathf.Min(70f * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
}
}
}
I also tried to change the Pivot/Center option in the editor but nothing helped so far.
I would attach the cannon to the turret, when the turret moves the cannon will move with it, this is how turrets work in reality. However if you did want to move the cannon and turret separately in sync then you need to move it's position and rotation.
http://answers.unity3d.com/questions/12704/hey-how-to-connect-two-objects-so-to-make-one.html
Answer by DwaynePritchett · Aug 05, 2017 at 08:16 PM
I don't know why your main camera is a child of the turret.
But, as for your problem, set the 'cannon' rotation to all zeros. In the video your cannon has a 'y rotation' of about 90 deg. Then leave the script on the turret body itself. That should do it.
You want the cannon barrel to be pointing out in the "forward" direction of the spherical turret body. From what I can see.
Your answer
Follow this Question
Related Questions
Why the cannon is never rotate looking at the target ? 1 Answer
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers