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 Juri · Nov 07, 2010 at 07:44 AM · collisioncharactercontroller

problems with OnControllerColliderHit

hey i try to make my enemy kill my, if he collides with my player. so here are the scripts i use for this (the first one is enemy and the second one is a script that only handles everything related to health):

var target : Transform; static var hitPoints : int = 50; var huntRange : float = 10.0; var rotationSpeed : float = 0.1; var moveSpeed : float = 3.0; var runSpeed : float = 5.0; private var speed : float;

function OnControllerColliderHit(hit : ControllerColliderHit) { var Hit = hit.collider; if(hit.gameObject.tag == "Player") { health.Hit = true; } }

function Start() { target = GameObject.FindWithTag("Player").transform; }

function Update() { var hit : RaycastHit; var direction : Vector3 = target.position - transform.position;

      if( direction.magnitude < huntRange || Physics.Raycast(transform.position , direction, hit)                            &&hit.collider.gameObject == target.gameObject)


         if(Player.running) speed = runSpeed;
         else speed = moveSpeed;

         transform.rotation = Quaternion.Slerp(transform.rotation,
                                 Quaternion.LookRotation(direction),
                                 rotationSpeed);
         transform.position += transform.forward * speed * Time.deltaTime;

}

and now the health script:

static var player_dead : boolean = false; static var Hit : boolean = false; var health : int = 340; var mana : int = 300; var recover : int = 10; var array = [health,mana, recover];

 function Update() {
     if(Hit == true && health >=0) {
             health -= enemy.hitPoints;
             if(health <= 0) {
                  health = array [0];
                  player_dead = true;
         }
     }
     else {
         health += recover * Time.deltaTime;
     }


     }
 function OnGUI () {
     GUI.BeginGroup(Rect(Screen.width / 2 - 550, Screen.height / 2 - 430 , 400, 100));
     GUI.Box(Rect(0,0,400,100),"Health");
     GUI.Box(Rect(30,20,health, 20),"");
     GUI.Box(Rect(30,60,mana, 20),"");
     GUI.EndGroup(); 
 }

Comment
Add comment · Show 1
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 sona.viswam · Apr 05, 2013 at 11:13 AM 0
Share

what is error ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Apexity · Apr 05, 2013 at 01:22 PM

Hey Juri,

Delete the Hit variable from inside the health script. If the health script is attached to the player then you can replace health.Hit = true; from inside your enemy script with Hit.GetComponent(health).health - //However much damage you want to deal; And, I believe the problem is forming from the naming of your variables. Which are both named Hit. Anywho, This should clean up your script a bit and should eliminate the problem. However, Unity isn't on this computer so I cant test it out right now. Get back to me if it continues to cause a problem. Hope this helped :D

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

2 People are following this question.

avatar image avatar image

Related Questions

Destroy character controller and instantiate new one 1 Answer

CharacterController "breaks" collision, goes haywire 2 Answers

Character controllers not colliding with each other 1 Answer

Error on checking collision or code desync? 0 Answers

Deform Charater controler Collider acording to stance 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