Tuesday, October 28, 2014

How To touch To orbit around player for Android in unity

Hi! readers I've recently converted the Mouse orbit Script that comes in standard assets to work with android and iOS touch devices. It's something very basic (easy to understand and modify) and very short. Just drag this script to the camera and assign a target around which you want the camera to rotate. Here it is...

//The standard mouse orbt script converted to work with touch devices (android and iOS) by Newtonians. Feel free to use or modify
//Check out the blog http://newtonians3d.blogspot.com/
using UnityEngine;
using System.Collections;

public class touchorbit : MonoBehaviour {
    private float x;
    private float y;
    public float xspeed;
    public float yspeed;
    private Touch touch;
    public float distance = 10;
    public Transform target;
    Rect margin;
    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        margin = new Rect(Screen.width * 0.5f, 0, Screen.width, Screen.height * 1.0f);
        float count = Input.touchCount;
        for (int i = 0; i < count; i++)
        {
            Touch tc = Input.GetTouch(i);
            if(margin.Contains(tc.position)){
            x += tc.deltaPosition.x * xspeed * 0.02f;
            y -= tc.deltaPosition.y * yspeed * 0.02f;
            }
        }
        Quaternion rotation = Quaternion.Euler (y, x, 0);
        Vector3 position = rotation * (new Vector3(0.0f, 0.0f, -distance)) + target.position;
       
        transform.rotation = rotation;
        transform.position = position;
    }
}


Another thing is that I've converted the original js script to c# so that it would be easier for a user to integrate the script into his projects.

This script is tested and works well but if you have some problem, please tell me.
Thanks.

2 comments: