prob to define a condition to get out of a search position loop for obstacle avoidance
hey at all,
i'm working on a obstacle avoidance script for a space shooter in 3 d world space without using colliders, bounds etc. My "test scene" contains 1 sphere (public thistransform) tagged as "Enemy" and a cube (size = 3.0,3.0,3.0) tagged as Obstacle and a transform as target. Another transform is attached to it as its child ( public thistarget). In the script the enemy detects if its closer to any obstacle when its in a defined range, kind of a sensor range ( = min_Abst). It basically works fine but if thistarget stops at a position in this range the enemy doesnt stop to "move around it". So i need to find a way to avoid this. I've tried so much but didn't find a solution.
btw sorry for my english it's even not easy to describe the prob and i hope it isn't totally confusing.
if someone would be so kindly trying to help me and make a simple scene: i placed the obst.cube at x = 0, y= 0, z = 50, so the target must be there + - min_Abst, attach the script to the enemy then you should immediately see what i mean.
thx very much previously!! for your affords
here's my script:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class FindOthers : MonoBehaviour {
 
     public GameObject[] Obstacles; 
     public GameObject[] Enemies;
     public List<Transform> AllOthers;
 
     public Transform target;        //main target
     public Transform thistarget;    // sub target = target.pos.x - 5
     public Transform thistransform; // sphere tagged as "Enemy"
 
     public float minimalDistance = 1.0f;
     public float speed = 10.0f;
     public float speedRotation = 10.0f;
 
     private Transform tr_allothers;
     private Vector3 targetPosition;
     private Vector3 thistargetabstand;
     private Vector3 Abstand;
     private Vector3 Abst_thistarg_to_Others;
     public float min_Abst = 5.0f;
 
     // Use this for initialization
     void Start () 
     {
         FindObstacles();
     }
 
     void Update()
     {
         UpDateList_AllOthers();
         //RotateTo (thistarget.position);
         //moveTo(thistarget.position);
 
         Vector3 thistargetabstand = thistransform.position - thistarget.position;
         if (thistargetabstand.magnitude >= minimalDistance)
         {
             RotateTo(thistarget.position);
             moveTo(thistarget.position);
         }
     }
 
 
     void FindObstacles()
     {
         Obstacles = GameObject.FindGameObjectsWithTag("Obstacle");
         Enemies = GameObject.FindGameObjectsWithTag("Enemy");
     }
         
     void UpDateList_AllOthers()
     {
         AllOthers = new List<Transform>(); // wichtig !! um Liste bearbeiten zu könen !
         int GetSiblingIndex = new int ();
         foreach(GameObject enemy in Enemies)
         {
             // damit wird ausgeschlossen,daßieses Enemy Objekt in AllOthers Liste aufgenommen wird !!
             Vector3 dist = thistransform.position - enemy.transform.position;
 
             if( dist.sqrMagnitude > 0.0f && enemy.activeSelf)
             {                
                 AllOthers.Add(enemy.transform); 
             }
         }
 
         foreach(GameObject obstacle in Obstacles)
         {
             if(obstacle.activeSelf)
             {
                 AllOthers.Add(obstacle.transform);
             }
         }
 
         foreach(Transform tr_allothers in AllOthers) 
         {    
             // the position of all transforms of this list in world space
             Vector3 AllOtherspos = new Vector3( tr_allothers.position.x, tr_allothers.position.y, tr_allothers.position.z);
 
             if(tr_allothers == null)
             {
                 AllOthers.RemoveAt(tr_allothers.GetSiblingIndex());// this removes this transform
             }
         }
 
         for (int i = 0; i <= AllOthers.Count-1;i++)
         {            
             GetSiblingIndex = i;
             Abstand = thistransform.position - AllOthers[i].position;
             Abst_thistarg_to_Others = thistarget.position - AllOthers[i].position;
 
             if(Abstand.magnitude <= min_Abst)
             {
                 if(thistransform.position.y >= AllOthers[i].position.y)
                 {
                     targetPosition = new Vector3 
                         (
                             (thistransform.position.x),
                             (AllOthers[i].position.y + ( AllOthers[i].lossyScale.y) + min_Abst),
                             (AllOthers[i].position.z - ( AllOthers[i].lossyScale.z - min_Abst))
                         );
                     RotateTo (targetPosition);
                     moveTo (targetPosition);
                 }
                 else
                 {
                     if(thistransform.position.y < AllOthers[i].position.y)
                     {
                         targetPosition = new Vector3 
                             (
                                 (thistransform.position.x),
                                 (AllOthers[i].position.y - ( AllOthers[i].lossyScale.y) - min_Abst),
                                 (AllOthers[i].position.z - ( AllOthers[i].lossyScale.z) - min_Abst)
                             );
                         RotateTo (targetPosition);
                         moveTo (targetPosition);
                     }
                 }
             }
         }
     }
 
 
     void RotateTo (Vector3 targetPosition)
     {
         Quaternion destRotation;
         Vector3 relativePos;
         relativePos = targetPosition - thistransform.position;
         destRotation = Quaternion.LookRotation (relativePos);
         thistransform.rotation = Quaternion.Slerp(thistransform.rotation, destRotation, speedRotation * Time.deltaTime);
     }
 
     void moveTo(Vector3 targetPosition) 
     {
         Vector3 moveDirection = thistransform.TransformDirection (Vector3.forward);
         Vector3 dist = targetPosition - thistransform.position;
 
         if (dist.magnitude > minimalDistance) 
         {
             thistransform.Translate(Vector3.forward * speed * Time.deltaTime);
 
         }
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Dot Product not equals 1 0 Answers
Click Vector3 position to change color 0 Answers
Player Floating 0 Answers
A position behind a moving gamobject but still inside another gameobject 0 Answers
need help spawning objects with specific cordinates 1 Answer
