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 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Enemy Attack Animation 1 Answer

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

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

How to create an AI follow script in Java? 2 Answers

AI Object Avoidance help 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