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 sayril · Nov 14, 2012 at 07:48 PM · collisionaiavoidance

What is the best way to avoid AI collision with my script?

I'm trying to make an AI script that performs zombie-like actions, but I'm running into a few problems. The first problem I had was that when I used transform.position = Vector3.MoveTowards(); to move the AI directly to the player (since I do not want the zombies to roam around, I want them to come directly towards the player),the two AI that I have run into each other and it seems as if they are transparent. I tried to fix this with a code from a tutorial I found by using way points. However, when I use the way points, the AI just stays in place and never moves. So, with the code below, what would be the best approach to this problem? I would like to learn how to code an AI, so please don't recommend programs to me. What I would like the AI to do is follow the player and move around obstacles in order to get to the player.

 @script RequireComponent(CharacterController)
 public var wayPoints : Waypoints;
 //Animations//
 public var _animation : Animation;
 public var runAnimation : AnimationClip;
 public var runAnimationSpeed : float = 2.0;
 public var idleAnimation : AnimationClip;
 public var idleAnimationSpeed : float = 1.0;
 //Get player... might be an array in the future.//
 public var player : Transform;
 public var AI : Transform;
 //Ragdoll//
 public var death : GameObject;
 //Movement Speeds//
 public var rSpeed : int = 9;
 public var wSpeed : int = 2;
 //Vectors//
 public var v3_targetDirection : Vector3;
 public var v3_moveDirection : Vector3;
 //Get Distance from character to player//
 public var distanceAway : float;
 //More Speeds//
 private var f_verticalSpeed : float = 0.0;
 private var f_moveSpeed : float = 0.0;
 //Health//
 public var aiHP : float = 100;
 private  var aiMaxHP : float;
 //Character Controller//
 private var controller : CharacterController;
 //Collison Flag//
 private var c_collisionFlag : CollisionFlags;
 //Movement Speeds//
 private var f_movementSpeed : float = 0.0;
 //Booleans//
 private var b_isStop : boolean;
 private var b_isAlive : boolean;
 //rotate//
 private var f_rotateSpeed : float = 1.2; //Smooth speed of rotation
 //weapon//
 var weapon : Rigidbody;
 var throwSpeed : float;
 
 
 function Awake() {
 controller = GetComponent(CharacterController);
 b_isAiming = false;
 b_isStop = false;
 b_isAlive = true;
 c_collisionFlag = CollisionFlags.CollidedBelow;
 aiMaxHP = aiHP;
 
 _animation[runAnimation.name].speed = runAnimationSpeed;
 _animation[runAnimation.name].wrapMode = WrapMode.Loop;
 _animation[idleAnimation.name].speed = idleAnimationSpeed;
 _animation[idleAnimation.name].wrapMode = WrapMode.Loop;
 
 
 
 }
 public function GetHpPercent() : float {
     return aiHP/aiMaxHP;
 }
 
 
 
 function Start () {
 transform.position = wayPoints.StartPosition();
 }
 
 function Update () {
 var v3_targetDirection : Vector3 = wayPoints.GetDirectionToPlayer(transform, player);
 GetHpPercent();
 _animation.CrossFade(runAnimation.name);
 if(aiHP <= 0){
 b_isAlive = false;
 }
 
 if(b_isAlive == true){
 distanceAway = GetDistance(AI, player);
 }
 if(distanceAway <= 5){
 
 _animation.CrossFade(idleAnimation.name);
 /*
 var  newProjectile : Rigidbody = Instantiate(weapon, AI.position, AI.rotation);
 newProjectile.rigidbody.velocity = transform.forward * throwSpeed;
 */
 rSpeed = 0;
 }else{
 rSpeed = 9;
 _animation.CrossFade(runAnimation.name);
 }
 if(v3_targetDirection != Vector3.zero){
 v3_moveDirection = Vector3.Slerp(v3_moveDirection, v3_targetDirection, f_rotateSpeed * Time.deltaTime);
 }
 var v3_movement : Vector3 = (v3_moveDirection * f_moveSpeed) + Vector3 (0, f_verticalSpeed, 0); // Apply the vertical speed if character fall down
 v3_movement *= Time.deltaTime;
 
 controller.Move(v3_movement);
 if(v3_moveDirection != Vector3.zero)
 {
 transform.rotation = Quaternion.LookRotation(v3_moveDirection);
 Debug.Log("You're moving!");
 }
 
 }
 
 function GetDistance (_AI : Transform, _player: Transform) {
 var distance : float = Vector3.Distance(_AI.transform.position, _player.transform.position);
 return distance;
 }
 
 
 function OnTriggerEnter(col : Collider){
 if(col.tag == "EnergySaw"){
 aiHP -= 20.0;
 if(aiHP <= 0){
 aiHP = 0;
 var ragdollPrefab : GameObject = Instantiate(death, transform.position, transform.rotation);
 
 }
 }
 }
 
 public function OnDrawGizmos() : void {
     if (player != null) {
         Gizmos.color = Color.blue;
         Gizmos.DrawLine(transform.position, player.position);
     }
 }

It's a lot of code, sorry about that. Thank you in advance!

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

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

Telling AI not to push each other around? 1 Answer

Avoidance Behaviour 2 Answers

without using NavMesh, How to avoid autonomous moving agents/obstacles 1 Answer

Resize Array Based on Value 2 Answers

A.I. Avoid Light 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