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 Ifxhitsy · Sep 25, 2014 at 12:47 PM · iosinputtouchcontrols

Touch Controls for a 2D ball game

I need some help for my test app, I just cant implement the right touch input methode to move the ball instead of the camera.

I have ball which is a prefab and gets cloned every second. I attached touch controls to the main camera with Raycast2D but it moves only the camera, not the ball which has a Rigidbody2D and a Collider2D. When I attach the touch controls to the ball prefab, it works, but it will move all ball game objects, not only the one which I touched. I tried several touch scripts, non of them suited for my game. Does someone have a script which will work for my ball game (attaching it to the main camera but only moving the ball I touched, not every ball - Rigidbody2D / Collider2D)?

I used this script first, it worked like I described above: // Moves object according to finger movement on the screen

     var speed : float = 0.1;
     function Update () {
         if (Input.touchCount > 0 && 
           Input.GetTouch(0).phase == TouchPhase.Moved) {
         
             // Get movement of the finger since last frame
             var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
             
             // Move object across XY plane
             transform.Translate (-touchDeltaPosition.x * speed, 
                         -touchDeltaPosition.y * speed, 0);
         }
     }

I also used the other ones from here. I tried to "mix" them to suit my style but failed.

My code looks like this now:

 using UnityEngine;
 using System.Collections;
 
 public class TouchTest2 : MonoBehaviour
 {
     public float moveSpeed = 0.1F;
     private RaycastHit2D hit;
     
     void FixedUpdate () 
     {
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) 
         {
             hit = Physics2D.Raycast(camera.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
             if(hit != null && hit.transform != null)
             {
                 Debug.Log(hit.transform.name);
                 Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
                 rigidbody2D.AddForce(new Vector2(-touchDeltaPosition.x * moveSpeed, -touchDeltaPosition.y * moveSpeed));
             }
         }
     }
 }

When I start the game I get the error that the main camera needs to have a Rigidbody2D attached to it but that doesnt make sense since I only want to move objects with Rigidbody2D (and Collider2D), not the camera. Anyone with a solution for my problem?

Comment
Add comment · Show 2
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 Ifxhitsy · Sep 26, 2014 at 09:02 PM 0
Share

Anyone please who can help me out? If there is a similar script available, can you post it here or the link where I can find it? $$anonymous$$aybe I need to change only some code to work with the script I made?

avatar image 767_2 · Sep 26, 2014 at 09:15 PM 0
Share

the error comes from which script which line

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do you map touch to one of the input axes? 1 Answer

I'm trying to translate this code to work for touch devices 0 Answers

Short touch delay using either Input or EventSystem classes 1 Answer

Why can't I press both my touch controls at the same time? 1 Answer

Input.touchCount won't return 2 or more on iOS. 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