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 turret 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.
It looks like in the 2nd part (where script is on the turret), the turret IS looking at the cube, but the cannon (the red thingy) is not on the correct side. In other words, you should put the cannon on the correct side of the turret ball; the side of the turret that’s looking towards the player. At 1:15, you can see you’ve got the cannon on the red (X) axis arrow. Try putting it on the blue (Z) axis arrow.
If i change the Cannon the red thing rotation to 0,0,0 the it will rotate facing the target fine but it also looks like not connected to the blue sphere(the body of the turret):
And i want it to looks like that but then it will not rotating fine: