先看效果:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotionAround : MonoBehaviour {
float x;
float y;
public float speed = 50f;
public Transform obj; //围绕的点
bool keepMove = false;
public float smoothness = 0.02f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0))
{
x = Input.GetAxis("Mouse X");
y = Input.GetAxis("Mouse Y");
if ((x > -0.2f && x < 0.2f) && (y > -0.2f && y < 0.2f))
{
return;
}
transform.RotateAround(obj.position, Vector3.up, x * speed * Time.deltaTime);
transform.RotateAround(obj.position, transform.right, -y * speed * Time.deltaTime);
StopCoroutine("StopMove");
}
if (Input.GetMouseButtonUp(0))
{
keepMove = true;
}
if (keepMove)
//惯性(缓动)
{
if (x > -0.001f || x < 0.001f)
{
StartCoroutine("StopMove");
}
x = x - x * smoothness;
y = y - y * smoothness;
transform.RotateAround(obj.position, Vector3.up, x * speed * Time.deltaTime);
transform.RotateAround(obj.position, transform.right, -y * speed * Time.deltaTime);
}
}
IEnumerator StopMove()
{
yield return new WaitForSeconds(4);
keepMove = false;
}
}