- Home /
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
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.