• 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 Jesterhead · Apr 27, 2012 at 06:52 AM · collisioncharacter controller

Colliding with Enemies

So I am currently trying to update my health bar GUI every time my main character collides with an object tagged "Hazard". I have a main character controlled with a Character Controller and a Stove with a Box Collider and Rigidbody so that I may text it. I have this code as my GUI for the health bar: //Variables for the texture to render on screen var fullHealth : Texture2D; var fourHealth : Texture2D; var threeHealth : Texture2D; var twoHealth : Texture2D; var oneHealth : Texture2D; var currentHealth : Texture2D;

//Item texture variables var itemOne : Texture2D; var itemTwo : Texture2D; var itemThree : Texture2D; var itemFour : Texture2D; var itemEmpty : Texture2D;

//boolean variables for if items are found or not var oneIsFound : boolean = false; var twoIsFound : boolean = false; var threeIsFound : boolean = false; var fourIsFound : boolean = false;

//position variables var position : Rect; var positionItemOne : Rect; var positionItemTwo : Rect; var positionItemThree : Rect; var positionItemFour : Rect;

function Start() { //set the health to the top of the screen position = Rect(0, 0, 100, 50 ); positionItemOne = Rect (480, 10, 60, 50); positionItemTwo = Rect (480, 60, 60, 50); positionItemThree = Rect (480, 110, 60, 50); positionItemFour = Rect (480, 160, 50, 60);

 //set the default health to Full
 currentHealth = fullHealth;

}

function OnGUI() { //Draw the current health bar GUI.DrawTexture( position, currentHealth );

 //draw the Items that are found when they are found
 if(oneIsFound){
 
     GUI.DrawTexture( positionItemOne, itemOne );
 }
 else{
     
     GUI.DrawTexture( positionItemOne, itemEmpty );
 }
 
 if(twoIsFound){
 
     GUI.DrawTexture( positionItemTwo, itemTwo );
 }
 else{
     
     GUI.DrawTexture( positionItemTwo, itemEmpty );
 }
 
 if(threeIsFound){
 
     GUI.DrawTexture( positionItemThree, itemThree);
 }
 else{
     
     GUI.DrawTexture( positionItemThree, itemEmpty );
 }
 
 if(fourIsFound){
 
     GUI.DrawTexture( positionItemFour, itemFour );
 }
 else{
     
     GUI.DrawTexture( positionItemFour, itemEmpty );
 }

} And then I have this code in my main Character to determine the Collision:


function Start () {

}

function Update () {

}

function OnTriggerEnter (collision: Collider){

 if(collision.gameObject.tag == "Hazard"){
     Debug.Log("COllision");
 }    

}

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 McDardy · Apr 27, 2012 at 07:41 AM 0
Share

Am I missing question part in this? Code isn't working? You get some errors? You should first read this question with answers: http://answers.unity3d.com/questions/18806/what-can-we-do-to-improve-the-quality-of-unityansw.html

2 Replies

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

Answer by Jesterhead · May 02, 2012 at 07:35 PM

Thanks all. It turns out the Character Controller has it's own Collider Method named OnControllerColliderHit(hit : ControllerColliderHit) The details can be found here.

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

Answer by Jesterhead · Apr 27, 2012 at 02:46 PM

Using the code I posted above, I can not get the collision to register between the Main Character and "Hazard" object and I was asking for some help.

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 fafase · Apr 27, 2012 at 02:58 PM 0
Share

Trivial question but just in case, is the hazard object tagged as "Hazard" or named as such? That might sound stupid but before going any further we need to make sure.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A Character Controller with another shape. 0 Answers

My Character Controller Is Warping Randomly (/w video) 0 Answers

Controller area of collision/contact 0 Answers

Not colliding with a box collider? 2 Answers

Character Controller giving false collisions 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