• 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 tomowale · Apr 20, 2017 at 08:55 AM · c#unity 5touchtouch controlstouchscreen

Make object follow my finger (Touch)?

Hey guys , I want to be able to move my object freely along the x and y axis with touch , so it can work on mobile phones. I have a script that works fine for PC but obviously won't for Android/IOS. So my question is , how can I get my object to move by the player dragging it with his finger by manipulating my script below? My game is 2D so I couldn't really use a previous script I created using raycasts etc.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class CarControl : MonoBehaviour {
     public float speed;
     Vector3 position;
     public float MaxPosX = 2.23f;
     public float MaxPosY = 3.69f;
     public GameObject anim  ;
      public UIManager ui;
      public AudioManager am;
  
     // Use this for initialization
     void Start () {
      
         am.carSound.Play();
         position = transform.position;
      }
  
     // Update is called once per frame
     void Update () {
      
  position.x += Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         position.y += Input.GetAxis("Vertical") * speed * Time.deltaTime;
         position.x = Mathf.Clamp(position.x, -2.23f, 2.23f);
         position.y = Mathf.Clamp(position.y, -3.69f, 3.69f);
         transform.position = position;
     }
     void OnCollisionEnter2D(Collision2D col) {
         if (col.gameObject.tag == "EnemyCar") {
             Instantiate(anim , transform.position , transform.rotation);
             am.explosionSound.Play();
             Destroy(anim);
             Destroy(this.gameObject);
             am.carSound.Stop();
             ui.gameOverorNot();
         }
     }
  
     }
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 _gord0_ · May 22, 2017 at 03:42 PM 0
Share

Did you ever sort this out?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tomowale · May 25, 2017 at 02:54 PM

Yes , I used a simple tap to move the car forward and once you release , it goes backwards. I use an acceloremeter to move left and right.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

When i press a button on the game scene its starts the game, how i can solve it ? 0 Answers

Unity Event Trigger iOS 1 Answer

EventSystem.current.IsPointerOverGameObject(t.fingerId) - Not working on Android builds - Unity 2 Answers

How to limit pinch scale of an object in AR? 0 Answers

Possible to get touch area data? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges