Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Ego65 · Mar 01, 2016 at 11:33 AM · positionvector3distancemovingobstacle avoidance

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);
 
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

51 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges