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 SudoSandwich · Aug 30, 2013 at 05:08 AM · movementcharactercontrollercontrollerenemy aislow

Enemy AI character controller movement is super slow

I cant figure out why the character controller on my enemy AI is moving super slow. I put a debug statement in the chase function to see what was happening. The controller is moving at (0.0,0.0,-0.1), even though the movementDirection vector is < or > 0 for x,y & z. It changes based on my postion, so the character at least turns to face me. When I wrote this in js it worked fine. In c# its giving me all kinds of trouble. The code is a hodge podge of my code and some from a tutorial I was following. I have spent a few hours researching and trying different things. Thanks in advance for the help.

 using UnityEngine;
 using System.Collections;
 
 public class AISimple : MonoBehaviour {
     
 public float Distance;
 public Transform Target;
 public float lookAtDistance= 25.0f;
 public float chaseRange= 15.0f;
 public float moveSpeed= 5.0f;
 public float Damping= 6.0f;
 public float fov= 160.0f;
 
 private RaycastHit hit;
 
 
 public CharacterController controller;
 public float gravity = 20.0f;
 private Vector3 moveDirection = Vector3.zero;
 
 /*
  * Is player in line of sight?
  */
 bool LineOfSight ( Transform target  ){
      if (Vector3.Angle(target.position - transform.position, transform.forward) <= fov &&
             Physics.Linecast(transform.position, target.position, out hit) &&
             hit.collider.transform == target) {
         return true;
     }
     return false;
 }
 
     
 /*
   This ai will fly and move through objects inlcuding terrain!
 */
 void  Update (){
 
         if (LineOfSight(Target) == true && hit.transform == Target) // Target is player
         {
             
             //enemy sees you - perform some action
             
             // Gauge the distance to the player. Line in 3d space. Draws a line from source to Target.
             Distance = Vector3.Distance(Target.position, transform.position);
             
             // Turn yellow enemy is getting close. Will chase soon.
             if (Distance < lookAtDistance)
             {
                 
                 lookAt();
             }
             
             // Safe distance
             if (Distance > lookAtDistance)
             {
                 renderer.material.color = Color.green;
             }
             
             // Attack! Chase the player until/if player leaves attack range.
             if (Distance < chaseRange)
             {Debug.Log("enemy chase");
                 chase ();
             }
         } 
         else 
         {
             //enemy doesn't see you
         }        
     }
 
 
 
 // Turn to face the player.
 void  lookAt (){
     renderer.material.color = Color.yellow;
     // Rotate to look at player.
     Quaternion rotation= Quaternion.LookRotation(Target.position - transform.position);
     // Dampening will slow the turn speed of enemy.
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 //    transform.LookAt(Target);
 }
 
 void  attack (){
     // in progress
     
 }
     
 void  chase (){
     renderer.material.color = Color.red;
     moveDirection = transform.forward;
     
     moveDirection *= moveSpeed;
     
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
     Debug.Log(moveDirection * Time.deltaTime);
         
 }
 }
Comment
Add comment · Show 3
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 getyour411 · Aug 30, 2013 at 05:29 AM 0
Share

nvm removed

avatar image SudoSandwich · Aug 30, 2013 at 05:36 AM 0
Share

I tried, no luck. I will just sleep on it. $$anonymous$$aybe it will come to me over night. Thanks.

avatar image getyour411 · Aug 30, 2013 at 05:46 AM 0
Share

Yeah I use Simple$$anonymous$$ove you use $$anonymous$$ove so it's a bit different

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by SudoSandwich · Aug 30, 2013 at 05:37 PM

Figured it out! The code is fine. My enemy was running into his own face! I removed all colliders except the character collider and it all works fine now.

Comment
Add comment · Show 4 · 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
avatar image SudoSandwich · Aug 30, 2013 at 05:17 PM 0
Share

only took ten hours...

avatar image _hrbharry_ · Feb 25, 2015 at 01:28 AM 0
Share

It takes an idiot to run into his own face xD

avatar image CastleIsGreat · Feb 25, 2015 at 04:55 AM 0
Share

I loled for real when I read this, glad you found the problem.

avatar image Orami · Sep 25, 2015 at 09:49 AM 0
Share

Exactly the same problem I was having except $$anonymous$$e liked walking sideways. Thanks.

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Character Controller moves forever after colliding with object 1 Answer

Jaggie, uneven speed with basic movement script. 0 Answers

Gradual Speed Increase with Variable Input 0 Answers

CharacterController unexpectedly moving up 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