Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NinjaRubberBand · Dec 10, 2016 at 02:52 PM · iphonetouchmultitouch

input.touch effect ever object script is attached to

For the last week ive been trying to make a script where i use the input.touch, to kill the object its attached to, when u press on the smartphone screen. The difference from mousebuttondown is, that i want to have the opportunity to tap and kill more than one enemy at a time. Ive read the physics.raycast2D, multitouch manuel, and seen a lot of answers and youtube video, but nothing has worked..

right now im working with this:

 using UnityEngine;
 using System.Collections;
  using System.Collections.Generic;
 
 public class testInput : MonoBehaviour {
 
     private RaycastHit2D hit;
     public int health = 3;
     private int speed = 2;
     
   
     
 void Update () 
     {
     
                 //transform.Translate(0,-speed * Time.deltaTime,0);       
 
         Touch myTouch = Input.GetTouch(0);
 
         Touch[] myTouches = Input.touches;
         for(int i = 0; i < Input.touchCount; i++)
         {
               hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.touches[i].position), Vector2.zero);
             
              if (Input.touches[i].phase == TouchPhase.Began) {
                  if(hit.collider.tag == "enemy") {
                       health = health-1;       
                       
                     
                   }
                  
              }
             
             if(health <= 0) {
                 die();
             
             }
              }
 }
     
   
     void die () {
     Destroy(hit.transform.gameObject);
 
         
     }
     
 
         
     
     
     }




The problem is, that it affects all the objects that the script is attached to, so every object gets -1 in health... I know its probably something with the tag, but i can't really find any other solution. Please help a man in pain.

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 NinjaRubberBand · Dec 11, 2016 at 03:22 PM 0
Share

Pls someone answer this. Im dying.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by edcx · Dec 10, 2016 at 03:56 PM

Yes, that's because you are checking hit objects tag. When ou clicked on one and all them throws ray and get the hit, they will all check wheter it is an enemy or not and they don't care if that enemy is themselves or not. Try this instead

 // get the collider which is the object you have touched and wanted to
 //destroy. Better save this collider in a variable ,so you don't use
 //getcomponent all the time        
 
 if (hit.collider == transform.GetComponent<Collider>() ) 
         {
             Destroy(hit.transform.gameObject);
         } 

hope that helps.

Comment
Add comment · 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 NinjaRubberBand · Dec 10, 2016 at 04:07 PM 0
Share

I tried replacing if(hit.collider.tag == "enemy") with if (hit.collider == transform.GetComponent() )

but it still doesn't work.

avatar image NinjaRubberBand · Dec 11, 2016 at 01:13 PM 0
Share

bump bump b

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

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

Related Questions

Input.GetTouch SOMETIMES not registering on iOS 0 Answers

iPhone multitouch 0 Answers

Multiple touch iPhone Buttons 0 Answers

iPhone Multitouch Problem 1 Answer

Game running on android but not in iphone 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