• 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 Mike-Cotton · Sep 21, 2012 at 12:37 PM · terrainaibuildingsraycastsavoid

Help with AI

Hello everyone, since I started back at college I have been working on my FMP. Up to now its going well I have setup quite a lot. You can see what I have done already here: http://www.youtube.com/channel/UCrOw2DA0dlAy4oMwIEzFsUA/

I've gotten to the point where I need to have my AI avoid buildings, walls, high terrain.

I've been looking at using raycasts to do this but I have no idea where to start.

My current AI

 //var for health and setting the amount
 //static var health : float = 100.0;
 //players transform
 var player : Transform;
 var playerGO : GameObject;
 //default speed of the enemy
 var speed = 5.0;
 //how far the enemy will attack from
 var chaseRange = 50.0;
 //attacking range
 var dieRange = 5.0;
 //last time enemy attacked
 var lastShotFired = 0;
 //how often the enemy can attack
 var attackTime = 2;
 //setting up var for the enemies controller(Makes it walk)
 private var controller : CharacterController;
 var bulletTraceGeneratorScript : GameObject;
 
 function Start()
 {
 playerGO = GameObject.FindWithTag("Player");
 player = playerGO.transform;
 
 bulletTraceGeneratorScript = GameObject.FindWithTag("BulletTrace");
 //gets the controller
     controller = GetComponent(CharacterController);
 //    yield MyWaitFunction (2.0); (OLD CODE)
 }
 
     
 function Update()
 {
 //if the players var is empty put nothing in there
     if (player == null)
         return;
 //How far away from the player the enemy is
     var range = Vector3.Distance(player.position, transform.position);
 //If the enemy is withtin attacking range(attack)
     if (range <= dieRange)
     {
 //if the time is 2(attacktime) past the lastshotfired then attack
    if (Time.time > lastShotFired + attackTime) {
     
     Debug.Log("Attacking");
     animation.Play("attack");
     animation["attack"].speed = 2;
     animation["attack"].wrapMode = WrapMode.Loop;
     lastShotFired = Time.time;
     player.GetComponent("health").health -= 10;
 
     }
     
 
 //    yield return new WaitForSeconds(attackTime);(OLDCODE)
 
     }
 //if it isnt in the attacking range
     else if (range >= chaseRange)
     {
     //enenmy looks at the player
         transform.LookAt(player);
     //setting up the var so the enemy knows which way to go
         var moveDirection : Vector3 = transform.TransformDirection(Vector3.forward);
     //moving the enemy toward the player (time.deltaTime*speed - making sure it is moving correctly)
 
        controller.Move(moveDirection * Time.deltaTime * speed);
      //this checks to see if the enemy is hovering above the ground
      //this is common in most AI as it only moves the enemy straight towards the player
      //this however fires a raycast at the floor which then adjusts the enemies position to
      //the hit point of the raycast
      
        //setting up the raycast var   
     var hit : RaycastHit;
     //if the raycast (from the enemy (-up) downwards) hits something below then it will
     //put the value in the hit var
     if (Physics.Raycast (this.transform.position, -Vector3.up, hit)) {
        //this is the distance from the hit point and the enemy
        distanceToGround = hit.transform.position;
        
        //DEBUG STUFF
        //print("Distance: "+distanceToGround);
        //print("Hitting "+hit.transform);
        
        //the commented line below draws a line in the editor so that I can check it is working
        //Debug.DrawLine (this.transform.position, hit.point, Color.red, 1);
        
        //this moved the enemies position from where it was(floating) to the floor
        this.transform.position = hit.point;
        
     }
 
     animation["RunninInPlace"].speed = 1;
     animation.Play("RunninInPlace");
         animation["RunninInPlace"].wrapMode = WrapMode.Loop;
         
     }
     //if it isnt doing any of the above stand still and play the idle animation
     else
     {
     //plays the idle animation
     animation.Play("idle");
     //loops the animation
     animation["idle"].wrapMode = WrapMode.Loop;
     }
 }

If anyone has any idea how I could implement raycasts to make my AI avoid stuff please help. I'm really stuck with this.

I don't want anyone to do this for me. Just give me a way of doing it.

Thanks for any help you can give :)

Mike

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MithosAnnar · Sep 21, 2012 at 12:42 PM

Hi Mike,

"Ray - Casting" is what you would want. Please have a look at these.

http://answers.unity3d.com/questions/318428/problems-with-raycast-obstacle-avoidance.html

http://answers.unity3d.com/questions/319836/weird-raycast-steering-problem.html

And that should help with adjacent objects. stopping at terrain is easy too. Stick an invisible boundary at the terrain with a box collider.

Cheers!

Mithos

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

Helicopter AI 1 Answer

can i use a structure i have designed in maya for terrain 1 Answer

People walking around 6 Answers

Heatmap for tracking enemy threat. 0 Answers

Extracting RGB component data from a textured plane positioned below a 3D terrain 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges