• 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 shrewdlove · Nov 25, 2011 at 09:08 AM · javascriptaiobjectsavoid

Adding Object Avoidance to my Enemy AI Script

I'm really having trouble adding the ability for my enemy to avoid onjects. I know I should be using some kind of ray-casting but I just can' figure it out. Any help is very much appreciated. Here is my code without the object avoiding:

var player : Transform; var speed : float=5; var range : float=15; var hitRange : float=6; var enemyDamage : float=10; var rotationSpeed : float=5; var damageTimer : float=0; var delta : Vector3; var distance;

function Update() { distance = Vector3.Distance(transform.position, player.transform.position); if(distance<=range) {
MoveTowards(); AttackPlayer(); } }

var moveSpeed = Vector3.zero; // this variable stores the current velocity var gravity: float = 20; // gravity like in the Move example

function MoveTowards() { if(distance<=(hitRange/1.5)) return; var character = GetComponent(CharacterController); if (character.isGrounded){ delta = player.transform.position - transform.position; delta.y = 0; // zero delta.y before normalization! delta.Normalize(); transform.forward = delta; // turn enemy to the player moveSpeed = delta speed; } moveSpeed.y -= gravity Time.deltaTime; // apply gravity acceleration character.Move(moveSpeed * Time.deltaTime); // move the character }

function AttackPlayer() { damageTimer+=Time.deltaTime; if (distance < hitRange && damageTimer>=1.5) { damageTimer=0f; player.SendMessageUpwards ("ApplyDamage", enemyDamage, SendMessageOptions.DontRequireReceiver); } }

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 Ben Ezard · Dec 13, 2011 at 10:08 PM

To use ray casting for object avoidance, what you do is draw, say, 5 rays out of the front of the enemy covering the front 180°, so that if, for example, a ray on the left hand side hits a collider, the enemy with rotate a bit to the right every frame until that ray is no longer hitting a collider Hope this helps a bit :-) A good place to learn how to use ray casting is the unity page below: http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

looking to make for a zombie script AI,navmesh or not? 2 Answers

Enemy Attack Animation 1 Answer

How do I create a simply disable script for enemy AI once it completes its waypoints? 2 Answers

AI Object Avoidance help 0 Answers

How Can I Change My Player Speed If A Variable Is Set To Zero 1 Answer

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