I am working on a 3d teaching project (not a gamer… sorry) in which I am using the following script:
using UnityEngine;
using System.Collections;
public class OrbitObject : MonoBehaviour {
public Transform target;
public float distance= 5.0f;
public float yMinLimit = -180f;
public float yMaxLimit = 180f;
public float FOVMin = 10;
public float FOVMax = 60;
public float smoothTime = 1f;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;//On_Swipe
//end of added
// Subscribe to events
void OnEnable(){
EasyTouch.On_PinchIn += On_PinchIn;
EasyTouch.On_PinchOut += On_PinchOut;
EasyTouch.On_Swipe += On_Swipe;
}
void OnDisable(){
UnsubscribeEvent();
}
void OnDestroy(){
UnsubscribeEvent();
}
// Unsubscribe to events
void UnsubscribeEvent(){
EasyTouch.On_PinchIn += On_PinchIn;
EasyTouch.On_PinchOut += On_PinchOut;
EasyTouch.On_Swipe -= On_Swipe;
}
void Start(){
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
{
rigidbody.freezeRotation = true;
}
}
// Zoom in and zoom out with pinch
void On_PinchIn(Gesture gesture){
float zoom = Time.deltaTime * gesture.deltaPinch*5;
Camera.mainCamera.fieldOfView=Mathf.Clamp((Camera.mainCamera.fieldOfView+=zoom), FOVMin, FOVMax);
}
void On_PinchOut(Gesture gesture){
float zoom = Time.deltaTime * gesture.deltaPinch*5;
Camera.mainCamera.fieldOfView=Mathf.Clamp((Camera.mainCamera.fieldOfView-=zoom), FOVMin, FOVMax);
}
//Swipe
void On_Swipe( Gesture gesture){
//Only if it is a single finger
if (gesture.touchCount==1 ){
rotationXAxis -= gesture.deltaPosition.y;//inverted after result on tablet
rotationYAxis += gesture.deltaPosition.x;//inverted after result on tablet
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
void LateUpdate()
{
//Allows mouse scrollwheel to control zoom as the mouse already controls the rest
if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit();
}
float zoom = Time.deltaTime * Input.GetAxis("Mouse ScrollWheel")*2000;
Camera.mainCamera.fieldOfView=Mathf.Clamp((Camera.mainCamera.fieldOfView-=zoom), FOVMin, FOVMax);
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
This script uses the Easy Touch plugin and it works well with tablets.
However, I need to change the target while running, using a GUI.
I basically have a number of objects that I need to make visible or invisible as the user selects the target (it is an anatomy course). I have been reading about GetComponent but I am lost!
Can someone shed light as I am a newbie?