Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Cscotttaakan · Apr 03, 2012 at 11:12 PM · aipathfinding

Ai Obstacle Avoidance

Essentially I am trying to make an object chase me. I want it to be able to chase me around walls and such. So far the chasing works, but the object still runs into the walls. I watched a video on it, and it seemed to help. Although, the character still eventually ends up floating because it runs into the wall so much. Video I Watched: vimeo.com/9304844

Here is my code - two scripts. Guard Script and sightScript. Guard script checkWall() function is where the main code is for the video.

guardScript:

pragma strict

var sight : Transform; var script : sightScript; var rotationSpeed : int = 8; var moveSpeed : int = 3;

var objectInFront = false;

private var myTransform : Transform; function Awake() { myTransform = transform; script = sight.transform.GetComponent(sightScript); } function Start () {

}

function Update () {

 if(script.see)
 {
     checkWall();
     if (!objectInFront)
     LookAt();
     
     Move();
 }

}

function Move() { myTransform.position += myTransform.forward moveSpeed Time.deltaTime; }

function LookAt() { myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(script.playerPos.transform.position - myTransform.position), rotationSpeed * Time.deltaTime);

}

function checkWall() {

 objectInFront = false;
 var dir : Vector3 = (script.playerPos.transform.position - transform.position).normalized;
 var hit :RaycastHit;
 if(Physics.Raycast(transform.position, transform.forward, hit, 20))
     {
         if(hit.transform != transform)
         {
             dir += hit.normal*20;
         }    
         Debug.DrawLine(transform.position, hit.point, Color.red);
 
         
         objectInFront = true;
     }
     
     var leftR : Vector3 = transform.position;
     var rightR : Vector3 = transform.position;
     
     leftR.x -= 1;
     rightR.x += 1;
     
     if(Physics.Raycast(leftR, transform.forward, hit, 20))
     {
         if(hit.transform != transform)
         {
             dir += hit.normal*20;
         }    
         Debug.DrawLine(leftR, hit.point, Color.red);
 
         
         objectInFront = true;
     }
     
     if(Physics.Raycast(rightR, transform.forward, hit, 20))
     {
         if(hit.transform != transform)
         {
             dir += hit.normal*20;
         }
         Debug.DrawLine(rightR, hit.point, Color.red);
         
 
         
         objectInFront = true;
     }
     
     if(objectInFront)
     {
     var rot : Quaternion = Quaternion.LookRotation(script.playerPos.transform.position - myTransform.position);
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, rot , rotationSpeed * Time.deltaTime);
     }
     
     

}

sightScript : //Script for detection of player, reactions to detection of player #pragma strict //Inspector Variables var mainObj : Transform; var playerPos : Transform; var see : boolean = false; //Private Variables private var myTransform : Vector3;

private var enterPlayerPos : Vector3;

function Awake() { myTransform = transform.position; } function Start () {

}

function Update () {

if (see) {

}

}

function OnTriggerEnter (other: Collider) { myTransform = transform.position; if (other.gameObject.tag == "Player") { Debug.Log("its working");

 var hit: RaycastHit;
     
     if (Physics.Linecast(myTransform, other.transform.position, hit))
     {
 
         enterPlayerPos = other.transform.position;
         
         if (hit.collider.tag == "Player")
         {
         
         Debug.Log("its working now");
         see = true;
         Debug.Log(see);
         
         }
     }
     
     
 
 }

}

function debug() {

Debug.DrawLine(myTransform, enterPlayerPos);

}

function OnTriggerExit (other : Collider) { if (other.gameObject.tag == "Player") { yield WaitForSeconds (5); see = false;

 }


}

Comment
Add comment · Show 1
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
avatar image Cscotttaakan · Apr 03, 2012 at 11:40 PM 0
Share

Regarding the main problem, if I have failed to address it, in the check wall function, it doesn't seem to be pushing the object back when it move in front of the wall. The rays show and are detecting the wall, but the object doesn't turn and isn't repelled.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kryptos · Apr 04, 2012 at 07:14 AM

Try to use UnitySteer which is a port of OpenSteer into Unity.

http://forum.unity3d.com/threads/26217-UnitySteer-steering-library-for-Unity

Comment
Add comment · Share
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Movement around a huge object by avoiding obstacles 1 Answer

Aron Granberg's A* seeker goes back and forth if a smooth modifier is attached. 0 Answers

Restricting NavMesh path finding directions 1 Answer

A* pathfinding for 2D top Down 0 Answers

Why isn't a Ai Waypoint Spawning??? 0 Answers


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