Turret wont stop to follow player, and instead pushes the player around

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;
       }
   }

}

Please look at this page & keep it in mind for future posts. You can edit your post with it, too: Using code tags properly

It will look much nicer, and be easier to read.

From your description, all that came to mind is make sure there isn’t something that can push the player before it can determine if the player is in the way? :slight_smile:

I didn’t attempt to read all of that code posted like that, though, sorry.

Thank you, i was trying to figure that out for a few minutes but posted shortly due to still being frustrated.

Regarding your response to my problem, the turret is using a empty game object “TurretSight” and then a child of that “TriggerSight” that is using a Sphere Collider to determine object detection and such. From the scene point of view, the sphere collider of the TriggerSight works, because when the turret detects an object, the sphere collider is immediately ontop of the object while the Turret is moving. HOWEVER… the coding for the Turret to STOP its rotating, and then just stare at the object isnt happening. Its set to Player, and i’ve messed around trying to create objects, naming them player, tagging them player, and so forth, but nothing is happening.

I’m glad you edited your post, but I’m still a little confused. Could just be tired, but if the turret is literally pushing the player, then you want to deal with that (if it’s some invisible part pushing it, anyways?).

I will try to check this later if you can explain. At first I thought it was about pushing, then you said it’s about not stopping (though, those could be related)… :slight_smile:

Maybe when my head is working better, sorry. Maybe someone will answer in the meantime.

I want the Turret to stop and stare at the intended target, but instead of that, the Turret continues to rotate, and the player is pushed and shoved out of the Turrets line of sight. Is the Sphere Collider the problem? I tried removing it, and that stops the player from being moved, but I think that takes away the Turrets ability to detect objects blocking its line of sight.

Okay, well a trigger collider can be good, but does it have the range (for line of sight being blocked) that you want? If so, ensure that you had it set to ‘isTrigger’: that shouldn’t be pushing the player, as that. Perhaps that was related to why it wasn’t working (and the pushing for sure).
If that’s the length/size you need, hopefully that fixes it.
If you needed longer, you could raycast or some other type of cast.