- Home /
Camera movement using touch input
Hello, I'm new to Unity and I'm currently trying to figure out how to move a camera using touch input on the X and Y axis for Android and iOS, Android as my testing bed though (Currently using Unity 3.5.6 as I only own the license for Android and iOS for that version)
I'm using the "Unity 3.x Game Development Essentials" book I got for learning, the first tutorial is making a basic "shoot blue ball at a wall of red cubes". Works with my 360 controller and Keyboard + mouse, now I'm trying to figure out touch input.
Heres the main code so far (in Javascript):
#pragma strict
function Start () {
}
var bullet : Rigidbody;
var power : float = 1500;
var moveSpeed : float =5;
function Update () {
var h : float = Input.GetAxis ("Horizontal") * Time.deltaTime * moveSpeed;
var v : float = Input.GetAxis ("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate(h, v, 0);
if(Input.GetButtonDown("Fire1"))
{
var instance: Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
}
}
That code in a basic game moves a camera and shoots a blue ball forward.
I read up on Touch input on the Unity Documentation, tried writing my own code, but didn't work (can post code if need be) and I couldn't figure it out.
I would like if possible least some code for me to use as reference so I can get a better understanding of Touch input for mobile devices.
Use this
function Update()
{
tap = false;
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
tap = touch.tapCount > 0 ? true : false;
/*Same as Input.GetButtonDown*/
if (tap)
{
var instance: Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
}
}
}
Or For quick Test use this
function Update()
{
if(Input.Get$$anonymous$$ouseButton(0))
{
var instance: Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
}
}
Or use both if you want
function Update()
{
#if UNITY_IPHONE || UNITY_ANDROID
tap = false;
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
tap = touch.tapCount > 0 ? true : false;
/*Same as Input.GetButtonDown*/
if (tap)
{
var instance: Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
}
}
#endif
if(Input.Get$$anonymous$$ouseButton(0))
{
var instance: Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
}
}