So I’m trying to recreate a script that has a turret scanning from left to right, and is supposed to stop when its sight is blocked by “Player” but when I test this, the player is getting pushed around by an invisible object while the turret continues to rotate left to right. This is also my first time posting on Unity forums, i have no idea how to format these topics correctly, and i’m pulling my hair out trying to figure out what i’m doing wrong
(This script is on the turret to have it spin, and if the other 2 scripts detect a player, activates the stop and stare at the player.)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LVD_TurretPivot : MonoBehaviour
{
public Transform leftTarget, rightTarget;
public float speed = 1.0f;
private bool flip = false;
//what do we see
public LVD_TurretSight tSight;
void FixedUpdate()
{
//if we do not see our target, rotate back and forth
if (tSight.seeTarget == false)
{
if (flip == false)
{
Vector3 oldRot = transform.rotation.eulerAngles;
//going left
//https://docs.unity3d.com/ScripRefernece/Vector3.RotateTowards.html
Vector3 newPos = leftTarget.position;
newPos.y = transform.position.y;
Vector3 targetDir = newPos - transform.position;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, speed * Time.deltaTime, 0.0f);
Debug.DrawRay(transform.position, newDir * 2, Color.blue);
transform.rotation = Quaternion.LookRotation(newDir);
if (transform.rotation.eulerAngles == oldRot)
flip = true;
}
else
{
Vector3 oldRot = transform.rotation.eulerAngles;
//going right
//https://docs.unity3d.com/ScriptRefernece/Vector3.RotateTowards.html
//modrightTarget position
Vector3 newPos = rightTarget.position;
newPos.y = transform.position.y;
Vector3 targetDir = newPos - transform.position;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, speed * Time.deltaTime, 0.0f);
Debug.DrawRay(transform.position, newDir * 2, Color.blue);
transform.rotation = Quaternion.LookRotation(newDir);
//have we reached our target direction?
if (transform.rotation.eulerAngles == oldRot)
flip = false;
}
}
else //we do see our target
{
//if our target is not empty
if (tSight.target != null)
{
//going to target location
//https://docs.unity3d.com/Scriptreference/Vector3.RotateTowards.html
//only rotate towards left and right, not up and down
Vector3 modTargetPos = new Vector3(tSight.target.position.x, transform.position.y, tSight.target.position.z);
Vector3 targetDir = modTargetPos - transform.position;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, speed * Time.deltaTime, 0.0f);
Debug.DrawRay(transform.position, newDir * 2, Color.blue);
transform.rotation = Quaternion.LookRotation(newDir);
}
}
}
}
(This script for the Turrets line of sight)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LVD_TurretSight : MonoBehaviour {
//What targets block vision?
public LayerMask mask;
//how far can i see
public float sightDistance = 10.0f;
//can I see the Target?
public bool seeTarget = false;
//trigger volume sight
public LVD_TriggerSight tSight;
//selevt target
public Transform target;
void FixedUpdate()
{
//set sight volume to origin
tSight.transform.localPosition = Vector3.zero;
//local raycasthit variable
RaycastHit hit;
//cast a ray out forward, at our distance, using our mask
if (Physics.Raycast(new Ray(transform.position, transform.forward), out hit, sightDistance, mask))
{
//if the collider isn't a trigger volume
if (hit.collider.isTrigger == false)
{
//set sight volume position to right in front of whatever we hit
tSight.transform.position = new Vector3(hit.point.x, hit.point.y, hit.point.z + tSight.radius);
}
}
//store from sight volume if we see the target
seeTarget = tSight.isHere;
//set our target to what we currently see
target = tSight.target;
}
}
(And this script is the sights detection radius.)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LVD_TriggerSight : MonoBehaviour {
//is something in our volume?
public bool isHere = false;
//what are we looking for?
public string tagToSeek = "Player";
//store our target
public Transform target;
//store our sphere collider so we can modify the radius
private SphereCollider sCol;
//variable for the radius
public float radius = 1.0f;
//at the beginning of the game
void Start()
{
//store the sphere collider
sCol = GetComponent<SphereCollider>();
}
//once every frame
void Update() {
//change the radius of the sphere collider to match our radius
sCol.radius = radius;
}
//something entered our volume
void OnTriggerEnter(Collider other)
{
//is the tag of the object we found the one we are looking for?
if (other.tag == tagToSeek)
{
//they are here!
isHere = true;
//store our target
target = other.transform;
}
}
//something left our volume
void OnTriggerExit(Collider other)
{
//is the tag of the object we lost the one were looking for?
if (other.tag == tagToSeek)
{
//they left...
isHere = false;
//dump the value of our target
target = null;
}
}
}