• 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
Question by RjToni · Sep 17, 2012 at 07:36 PM · collisiontriggerai

Funny error. collision just hit 12 times

Hey, Guys!

Well... that's odd, but I'll try to explain. I have a robot with a box collider in $$anonymous$$s sword. And I have a Script attached to my player that checks the collision with the enemy's sword, And so far, it's barely ok. Because I have 2 issues.

First. When the robot's sword $$anonymous$$ts the player, the collision checks twice or more, but I'm gonna let this way, maybe I just expand the player health, though.

Second. The robot saw me, run at me and start to attack. every $$anonymous$$t will collide and drop my health, but after the 12nd $$anonymous$$t, the collision doesn't works anymore. He's just swinging $$anonymous$$s sword, and the collisions backs to work when I move the player.

Very odd, huh? Is that happened before? Well, I'm posting my scripting so far.

The Enemy AI script:

 var waypoint : Transform[];        // The amount of Waypoint you want
 
             var patrolSpeed : float = 3;       // The walking speed between Waypoints
 
             var loop : boolean = true;       // Do you want to keep repeating the Waypoints
 
             var player : Transform;          // Referance to the Player
 
             var dampingLook = 6.0;          // How slowly to turn
 
             var pauseDuration : float = 0;   // How long to pause at a Waypoint
 
             var attackRange = 10;          // Range to start the attack
 
             var attackSpeed = 5.0;          // Speed to attack
 
             var attackLook = 10.0;          // How fast to turn when attacking
 
             var AtkSpeed = 1;
 
             
 
 
 
             private var distanceToPlayer : int; // Distance from the enemy to the Player
 
             private var curTime : float;
 
             private var currentWaypoint : int = 0;
 
             private var character : CharacterController;
 
             private var gravity : float = 2.0;
 
             private var attacking : boolean = false;
 
             private var isattacking : boolean = false;
 
 
 
             function Awake(){
 
 
 
             }
 
 
 
             function Start(){
 
             
 
                 
 
                 character = GetComponent(CharacterController);
 
                 animation["attack"].speed = AtkSpeed;
 
                 animation["attack"].layer = 1;
 
                 
 
                 
 
                 
 
             }
 
 
 
             function Update(){
 
             
 
             
 
 
 
         distanceToPlayer = Vector3.Distance(player.position, transform.position);  // Distance between the enemy and the Player
 
 
 
         if(distanceToPlayer < attackRange){
 
            attacking = true;
 
            attack();
 
            }else{
 
            attacking = false;
 
            }
 
            
 
         
 
 
 
         if(currentWaypoint < waypoint.length && !attacking){
 
            patrol();
 
            }else{    
 
         if(loop && !attacking){
 
            currentWaypoint=0;
 
             } 
 
         }
 
     }
 
 
 
             function patrol(){
 
 
 
                     var target : Vector3 = waypoint[currentWaypoint].position;
 
                     target.y = transform.position.y; // Keep waypoint at character's height
 
                     var moveDirection : Vector3 = target - transform.position;
 
 
 
                 if(moveDirection.magnitude < 0.5){
 
 
 
                    if (curTime == 0) {
 
                      curTime = Time.time; // Pause over the Waypoint
 
                      animation.Play("idle");
 
                      }
 
                    if ((Time.time - curTime) >= pauseDuration){
 
                      currentWaypoint++;
 
                      curTime = 0;
 
                    }
 
                 }else{        
 
             // Look at and dampen the rotation
 
                    var rotation = Quaternion.LookRotation(target - transform.position);
 
                    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
 
                    character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
 
                    animation.Play("walk");
 
                 }  
 
             }
 
 
 
             function attack(){
 
             // Attack the Player
 
             if (distanceToPlayer <= 1) {
 
             animation.Play("attack");
 
             }
 
             else {
 
             animation.Stop("attack");
 
             }
 
                
 
             // Rotate to face the Player
 
             var lookPos = player.position - transform.position;
 
                 lookPos.y = 0;
 
                 var rotation = Quaternion.LookRotation(lookPos);
 
                 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * attackLook);
 
 
 
             // Move towards the Player
 
                 var target : Vector3 = player.position;
 
                 var moveDirection : Vector3 = target - transform.position;
 
 
 
                 moveDirection.y = 0; // Stop the character running up off the floor to match the Players Y axis
 
                 moveDirection.y -= gravity; // Add gravity so the he always stays on the ground
 
                 character.Move(moveDirection.normalized * attackSpeed * Time.deltaTime);
 
                 animation.Play("run");
 
             }
 
             
 
             
 

 And that's is the script that checks the collision the player with the sword

 var PlayerLife = 3;
 var player : Transform;
 private var m_isHit : boolean = false;
 
 
 //function OnControllerColliderHit($$anonymous$$t: ControllerColliderHit){
 
 function OnTriggerExit (other : Collider) {
 if(other.gameObject.tag == "attackPoint"){
 PlayerLife -= 1;
  
 } 
 
 }
 //}
 
 
 
 function Update () {
 
 print ("Health :" +PlayerLife);
 }
Comment

People who like this

0 Show 0
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

Answer by justinl · Sep 19, 2012 at 05:24 AM

Maybe your distanceToPlayer variable becomes larger than 1.

Comment

People who like this

0 Show 2 · 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 RjToni · Sep 19, 2012 at 02:40 PM 0
Share

That's make sense... should I raise distance in the if statment?

avatar image justinl · Sep 19, 2012 at 03:19 PM 0
Share

Give it a shot. I'd Debug.log(distanceToPlayer ); on your update so you can see what's going on there to isolate if that variable is changing and causing your script to misbehave

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

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

Have falling object exit from a collider after collision? 2 Answers

Terrain doesn't collide with my mesh 1 Answer

Can not get value out of a collision 1 Answer

OnTriggerEnter - Same collider triggers multiple times?, is size a factor? 4 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