Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 7th, 2012  |  syntax: C#  |  size: 1.23 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MotionControl : MonoBehaviour
  5. {
  6.     public GUIStyle MyStyle;
  7.     public Texture Joystick;
  8.     private float translationZ = 0f;
  9.     private float translationX = 0f;
  10.  
  11.     void OnGUI()
  12.     {
  13.         GUI.Button(new Rect(0, Screen.height - Joystick.height, Joystick.width, Joystick.height), Joystick, MyStyle);
  14.     }
  15.  
  16.     // Use this for initialization
  17.     private void Start()
  18.     {
  19.     }
  20.  
  21.     public float speed = 100.0F;
  22.    
  23.  
  24.     private void Update()
  25.     {
  26.         translationZ = Input.GetAxis("Vertical") * speed;
  27.         translationX = Input.GetAxis("Horizontal") * speed;
  28.  
  29.         if (Input.GetMouseButton(0))
  30.         {
  31.             Debug.Log(Input.mousePosition.x < Joystick.width);
  32.             if ((Input.mousePosition.y < Joystick.height) && (Input.mousePosition.x < Joystick.width))
  33.             {
  34.                 translationZ = (Input.mousePosition.y - Joystick.height/2)*speed/20;
  35.                 translationX = (Input.mousePosition.x - Joystick.width/2)*speed/20;
  36.             }
  37.         }
  38.         translationZ *= Time.deltaTime;
  39.         translationX *= Time.deltaTime;
  40.        
  41.         transform.Translate(translationX, 0, translationZ);
  42.        
  43.     }
  44. }