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 Iceblitzyt · May 07, 2013 at 06:45 PM · c#aienemy

Why doesn't this AI script work? c#

hey there,

So I'm creating a object that moves back and forth from 2 points, i wanted to make it if the player touched the gameobject it loses health.

I made an empty gameobject with 3 different spheres attached to it.

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class Enemy2AI : MonoBehaviour {
     public Transform StartPoint;
     public GameObject Player;
     public Transform Endpoint;
     public bool DirectionSwap; 
     public float Speed;
     
     void FixedUpdate () {        
     if(transform.position == Endpoint.position){
     DirectionSwap = true;        
     }
     if(transform.position == StartPoint.position){
     DirectionSwap = false;        
     }    
         
     if(DirectionSwap){
     transform.position = Vector3.MoveTowards(transform.position,StartPoint.position,Speed);
     }
     else{
     transform.position = Vector3.MoveTowards(transform.position, Endpoint.position,Speed);        
         }
     
     if(Player.transform.position == gameObject.transform.position){
     HealthBar hb = Player.GetComponent<HealthBar>();
     hb._Currenthealth -= 10;                    
     }
         
         
     }
 }

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 TonyLi · May 07, 2013 at 07:02 PM 0
Share

Please specify what doesn't work. What do you expect it to do, and what is it doing ins$$anonymous$$d?

You're probably going to want to use Physics colliders to detect collisions (touches): http://docs.unity3d.com/Documentation/$$anonymous$$anual/Physics.html

avatar image Iceblitzyt · May 07, 2013 at 07:05 PM 0
Share

Sorry, I thought i wrote it out.

The back and forth works, i don't get why it wont deduct health when the position of the player is equal to the object.

avatar image TonyLi · May 07, 2013 at 08:01 PM 0
Share

It's extremely unlikely that the positions will be exactly the same. See my suggested answer below.

1 Reply

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

Answer by TonyLi · May 07, 2013 at 08:04 PM

Two vectors in 3D space consisting of 3 floats each will almost never be exactly equal.

Instead, attach colliders to the player and the object.

In a script on one of the objects, include:

 void OnCollisionEnter(Collision collision) {
     HealthBar hb = Player.GetComponent<HealthBar>();
     hb._Currenthealth -= 10; 
 }

Reference: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

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 Iceblitzyt · May 08, 2013 at 04:36 PM 0
Share

Still doesn't work. I even altered it to make it:

     void OnCollisionEnter(Collision collision) {
     if(collision.gameObject.name == "Player Controller "){
     HealthBar hb = Player.GetComponent<HealthBar>();
     hb._Currenthealth -= 10; 
     }
     }
avatar image Iceblitzyt · May 08, 2013 at 04:52 PM 0
Share

Don't worry I fixed it and used the on trigger func as that worked better for what i needed it for. Anyways thanks for pointing me in the right direction. :)

avatar image TonyLi · May 08, 2013 at 04:55 PM 1
Share

Try adding some debug lines to see where it's failing:

 void OnCollisionEnter(Collision collision) {
 
     Debug.Log("1. Collided with " + collision.gameObject.name);
 
     if (collision.gameObject == Player) {
         Debug.Log("2. This is the player.");
 
         HealthBar hb = Player.GetComponent<HealthBar>();
         if (hb == null) Debug.Log("3. HealthBar is NULL");
 
         hb._Currenthealth -= 10;
     }
 }


If you get to Debug #1, you know the collision worked.

Note that I changed the player check conditional in two ways:

  1. Compares the gameObject with Player, since you already have it. This may or may not be correct depending on your implementation.

  2. Got rid of the string comparison. I noticed that in your example code the string has a space at the end -- "Player Controller "

If you get to Debug #2, you know the collision hit the player object.

If you get Debug #3, then it didn't find HealthBar on the player object.

avatar image Iceblitzyt · May 09, 2013 at 01:51 PM 0
Share

Thanks tony it's working now and polished, you're a star :)

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

13 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

Related Questions

Make player not be seen by AI, when player in foilage and shadows. 1 Answer

Enemy AI C# 0 Answers

Multiple Cars not working 1 Answer

C# AI, Enemy is flying at me when I jump. 2 Answers

2D AI, Aim at player - even when jumping? 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