- Home /
Why the cannon is never rotate looking at the target ?
What I want is to make some ai that the cannon/turret will rotate automatic facing the target. But when I'm running the game and moving the target in the scene view window the cannon is facing the other direction and not to the target.
The script to rotate:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateTurret : MonoBehaviour
{
[SerializeField]
private float turnRateRadians = 2 * Mathf.PI;
[SerializeField]
private Transform turretTop; // the gun part that rotates
[SerializeField]
private Transform bulletSpawnPoint;
[SerializeField]
public GameObject target;
void Update()
{
TargetEnemy();
}
void TargetEnemy()
{
if (target != null)
{
Vector3 targetDir = target.transform.position - transform.position;
// Rotating in 2D Plane...
targetDir.y = 0.0f;
targetDir = targetDir.normalized;
Vector3 currentDir = turretTop.forward;
currentDir = Vector3.RotateTowards(currentDir, targetDir, turnRateRadians * Time.deltaTime, 1.0f);
Quaternion qDir = new Quaternion();
qDir.SetLookRotation(currentDir, Vector3.up);
turretTop.rotation = qDir;
}
}
}
And this is a screenshot showing where the script is attached to and when I'm moving with them ouse in the scene view the target the cube backward and forward not yet around the cannon the cannon is always facing the other direction:
And this is when running the game right after the game is running the turret automatic facing the other way and when moving the target the cube the turret will keep rotating facing the other direction:
Answer by KittenSnipes · Apr 03, 2018 at 12:47 AM
Well I only changed one part. So the function takes an input of a GameObject which will be the target object. Hope it works out for you.
using UnityEngine;
public class RotateTurret : MonoBehaviour
{
[SerializeField]
private float turnRateRadians = 2 * Mathf.PI;
[SerializeField]
private Transform turretTop; // the gun part that rotates
[SerializeField]
private Transform bulletSpawnPoint;
[SerializeField]
public GameObject target;
void Update()
{
TargetEnemy(target);
}
void TargetEnemy(GameObject target)
{
if (target != null)
{
Vector3 targetDir = target.transform.position - transform.position;
// Rotating in 2D Plane...
targetDir.y = 0.0f;
targetDir = targetDir.normalized;
Vector3 currentDir = turretTop.forward;
currentDir = Vector3.RotateTowards(currentDir, targetDir, turnRateRadians * Time.deltaTime, 1.0f);
Quaternion qDir = new Quaternion();
qDir.SetLookRotation(currentDir, Vector3.up);
turretTop.rotation = qDir;
}
}
}
Your answer
Follow this Question
Related Questions
How can i rotate object smooth without stop ? 2 Answers
Why when using LookAt to make the turret facing the target it's facing the other direction ? 1 Answer
How can i rotate both turret cannon and body same time or separate ? 1 Answer
How can i switch smooth between humanoidwalk and humanoididle states ? 1 Answer
How can i move object between given random positions ? 0 Answers