i'm trying to make an iOS game in which i want the game to run only when the user is touching the screen and to pause as soon as he removes his hand off the screen.i tried using time.timescale in the manner below but i believe it doesn't affect the code in the Update() function .I need to find another way to do this as it doesn't work the way i want it to.some help?
using UnityEngine;
using System.Collections;
public class loco : MonoBehaviour {
private Transform myTransform; // this transform
private Vector3 destinationPosition; // The destination Point
private float destinationDistance; // The distance between myTransform and destinationPosition
public float moveSpeed; // The Speed the character will move
void Start () {
myTransform = transform; // sets myTransform to this GameObject.transform
destinationPosition = myTransform.position;
// prevents myTransform reset
}
void Update () {
Time.timeScale = 0.0f;
// keep track of the distance between this gameObject and destinationPosition
destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
if(destinationDistance < .5f){ // To prevent shakin behavior when near destination
moveSpeed = 0;
}
else if(destinationDistance > .5f){ // To Reset Speed to default
moveSpeed = 30;
}
// Moves the Player if the Left Mouse Button was clicked
if (Input.GetMouseButtonDown(0)&& GUIUtility.hotControl ==0) {
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
}
}
// Moves the player if the mouse button is hold down
else if (Input.GetMouseButton(0)&& GUIUtility.hotControl ==0) {
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
}
myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
}
// To prevent code from running if not needed
//if(destinationDistance > .5f){
// myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
//}
if (Input.GetMouseButton (0) || Input.touchCount > 0) {
Time.timeScale=1.0f;
}
}
}
↧