- Home /
Move object using swipe control for mobile
I got this script from the unity forum ,i have attached this script to Cube in unity ,it works fine in WASD keys in desktop, but how can i get the same output in (mobile) swipe controls .If i swipe left in mobile the object should move Left,and if i swipe Right Object should move Right. As i am new to Programming , if anyone could provide codes for this would be very appreciated. Please find the below code
using UnityEngine;
using System.Collections;
public class Swipe : MonoBehaviour {
public float speed = 1; // speed in meters per second
public float smooth = 2.0F;
public float tiltAngle = 30.0F;
// Update is called once per frame
void Update () {
Vector3 moveDir = Vector3.zero;
moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
moveDir.z = Input.GetAxis("Vertical"); // get result of WS keys in Z
// move this object at frame rate independent speed:
float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
transform.position += moveDir * speed * Time.deltaTime;
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
transform.localPosition += transform.forward * speed * Time.deltaTime;
}
}
Comment
Answer by MOSTY · Mar 31, 2016 at 09:50 PM
Or just use this asset. https://www.assetstore.unity3d.com/en/#!/content/31255