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 Lachee1 · Aug 09, 2012 at 11:23 PM · ailagcharacter controllerzombiemotor

Zombie AI Lag when checking if it should jump

Hello, I have made a very simple Zombie AI script, but i am having trouble with jumping. The current code i have works fine, but the second i have 3+ zombies jumping, unity lags extreamly bad, is there a way to stop this? is it just because i am calculating to many collisions? this is my code:

 var Player : GameObject;
 var Waypoint : Vector3 = Vector3.zero;
 var inMob : boolean = false;
 var isLeader : boolean = false;
 var rotSpeed : float = 1;
 var SpottingDistance : float = 10;
 var Dis : float = 100;
 var motor : CharacterMotor;
 private var controller : CharacterController;
  var AttackPlayer : boolean = false;
 function Start(){
  Player = GameObject.FindWithTag("Player");
  controller = GetComponent(CharacterController);
  motor = GetComponent(CharacterMotor);
 }
 
 function FixedUpdate(){
 
  if (!AttackPlayer){
     
  if (Vector2.Distance(Vector2(transform.position.x,transform.position.z),Vector2(Waypoint.x,Waypoint.z)) < 15){
  AssignWaypoint(Vector3.zero);
  }else{
  var WaypointDirection = Waypoint - transform.position;
  //controller.SimpleMove(WaypointDirection);
  
  motor.inputMoveDirection = Vector3.ClampMagnitude(WaypointDirection,1);//transform.rotation * WaypointDirection;
  }
  }else{
  
  
  var PlayerDirection = Player.transform.position - transform.position;
  //controller.SimpleMove(WaypointDirection);
  
  motor.inputMoveDirection = Vector3.ClampMagnitude(PlayerDirection,1);//transform.rotation * WaypointDirection;
  }
 
 
 
 var loc : Vector3 = Waypoint;
 var PlayerDirectionFixed = Player.transform.position - Vector3(0,0.5,0) - transform.position;
 var Yofset : float = 1.5;
 
 
  Dis = Vector3.Distance(Vector2(Player.transform.position.x,Player.transform.position.z),Vector2(transform.position.x, transform.position.z));
      if (Dis < SpottingDistance){
       if (Dis < 16)
       Yofset = 1;
       
       if (Dis < 5.8)
       Yofset = 1;
       
       
       
       
       AttackPlayer = true;// Incase the ray doesn't work
       var hit : RaycastHit;
      if (Physics.Raycast (transform.position + Vector3(0,Yofset,0), PlayerDirectionFixed, hit, SpottingDistance+2)){//Ignore the player
      if (hit.transform.tag != "Player" && hit.transform.tag != "MainCamera" && hit.transform.tag != "IgnoreRay"){
      AttackPlayer = false;
      
      
      Debug.DrawRay (transform.position + Vector3(0,Yofset,0), PlayerDirectionFixed, Color.red);  
      }else{
      AttackPlayer = true;
          Debug.DrawRay (transform.position + Vector3(0,Yofset,0), PlayerDirectionFixed, Color.green);  
      }       
      }else{
          AttackPlayer = true;
          Debug.DrawRay (transform.position + Vector3(0,Yofset,0), PlayerDirectionFixed, Color.green);         
          }
      }else{
       AttackPlayer = false;
      }
      
     if (AttackPlayer)
      loc = Player.transform.position;
      
    // if (AttackPlayer)
    // Waypoint = Player.transform.position;//to come back the 'I have better waypoints to go to' glich
     
     
      
     var targetRotation = Quaternion.LookRotation (loc - transform.position, Vector3.up);
  transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
  transform.rotation.x = 0; transform.rotation.z = 0; // rotating it towards the target/waypoint
  
    
 }
 
 function AssignWaypoint(location : Vector3){
  if (location != Vector3.zero){
  Waypoint = location;
  }else{
  var Rnd : Vector3 = Vector3.zero;
  Rnd.x = Random.Range(-300,300);
  Rnd.z = Random.Range(-300,300);
  Rnd.y = 0;
  Waypoint = Rnd;
  }
 }
 
 function OnControllerColliderHit (hit : ControllerColliderHit) {
 
    
 if (hit.moveDirection != Vector3.zero && hit.moveDirection.y == 0){
  //var wallHit : RaycastHit;
     // if (Physics.Raycast (transform.position + transform.forward, transform.forward, wallHit, 3)){//Ignore the player
     // if (wallHit.transform.gameObject == null){
     // 
     // Debug.DrawRay (transform.position + transform.forward, transform.forward, Color.blue);  //add jump here
     // motor.inputJump = true;
     // 
     // }else{
     // Debug.DrawRay (transform.position + transform.forward, transform.forward, Color.magenta);  
     // motor.inputJump = false;
     // }   
     // 
  // }
  
  Debug.DrawRay (transform.position + transform.forward, transform.forward, Color.blue);  //add jump here
     motor.inputJump = true;
     yield WaitForSeconds(0.5);
    motor.inputJump = false;
     
 }
 }
 
 
 
 @script RequireComponent (CharacterMotor)
 @script AddComponentMenu ("Character/FPS Input Controller")


Tank you in advance, Lachee

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 sam32x · Aug 10, 2012 at 07:34 AM

 function Start () {
 Invoke("Loop",0.1);
 }
 
 function Loop () {
 Invoke("Loop",0.1);
 //lagging code goes here
 }

that's how i fix code that lags

Comment
Add comment · Show 1 · 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 Lachee1 · Aug 11, 2012 at 09:57 AM 0
Share

Thanks, ill give it a try.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simple movement causing lag (on standalone) 1 Answer

Ai Zombie Melee Attack script. 5 Answers

Why is this script causing a lot of lag? 2 Answers

Way Point Script Change Group 0 Answers

Character Motor causes serious lag. 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