How can I make my turret also rotate on the X-axis?
I am following a part of a tutorial from Brackeys on making a Tower-defense game, where they code-in the turret AI for engaging targets, but I'm using it in my own game, to create hostile turrets, which target the player. I thought adding a rotation.x instead of a 0 on partToRotate.rotation = Quaternion.Euler(rotation.x, rotation.y, 0f); would fix it, but it still only rotates on Y only. I have looked at it to search for any syntax mistakes, that I'd be able to fix, but it all seems fine to me, so why doesen't it rotate on the Y axis as well?
If someone gives me an answer, can you also just quickly explain why this will not work? I am checking the documentation and am a bit stumped, atough I'm a complete newbie.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SentryAI : MonoBehaviour
{
public Transform targetingPlayer;
public float range = 15f;
public Transform partToRotate;
public float sentrySpeed = 5f;
public string playerTag = "Player";
void Start()
{
InvokeRepeating("UpdateTarget", 0f, 2f);
}
void UpdateTarget()
{
// Only some variable names were changed, since I want the turrets to target the player
GameObject[] enemies = GameObject.FindGameObjectsWithTag(playerTag);
float shortestDistance = Mathf.Infinity;
GameObject nearestEnemy = null;
foreach (GameObject enemy in enemies)
{
float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
if (distanceToEnemy < shortestDistance)
{
shortestDistance = distanceToEnemy;
nearestEnemy = enemy;
}
}
// Target nearest enemy - may be unnecessary
if (nearestEnemy != null && shortestDistance <= range)
{
targetingPlayer = nearestEnemy.transform;
}
else
{
targetingPlayer = null;
}
}
void Update()
{
if (targetingPlayer == null)
return;
// This is the lock-on
Vector3 dir = targetingPlayer.position - transform.position;
Quaternion lookRotation = Quaternion.LookRotation(dir);
Vector3 rotation = Quaternion.Lerp(partToRotate.rotation, lookRotation, Time.deltaTime * sentrySpeed).eulerAngles;
partToRotate.rotation = Quaternion.Euler(rotation.x, rotation.y, 0f);
}
private void OnDrawGizmosSelected()
{
Gizmos.DrawWireSphere(transform.position, range);
}
}
This is the code.
Also additionaly - will partToRotate.x = Mathf.Clamp(turretTurn.x, minRotationX, maxRotationX); work with the turret, if I want to clamp the angles on which it is able to rotate, like in my other script for turning the player's turret?
Thanks a lot for any replies. The few answers on here that I got on my last post helped me learn a lot <3 I have checked multiple forums and videos for answers, but I cannot figure it out in time for a gamejam.
Your answer
Follow this Question
Related Questions
Copy Rotation in script C# 1 Answer
Searching a function to get the rotation of one Vector3 in relation to another 1 Answer
How do you continuously rotate an object? 1 Answer
Why won't my model rotate up on X axis? 1 Answer
How do i limit the rotation of a game object in relation to a free following camera ? 0 Answers