物体始终面向主摄像机脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAtCamera : MonoBehaviour
{
[Header("面向的摄像机Camera")]
public Camera cameraLookAt;
[Header("选择需要固定的轴")]
[Tooltip("可以自由选择固定不变的轴,常用的选泽是None或者Y")]
public SelectXYZ selectXYZ = SelectXYZ.None;
void Update()
{
if (cameraLookAt == null)
cameraLookAt = Camera.main;
Vector3 vector3 = cameraLookAt.transform.position - transform.position;
switch (selectXYZ)
{
case SelectXYZ.x:
vector3.y = vector3.z = 0.0f;
break;
case SelectXYZ.y:
vector3.x = vector3.z = 0.0f;
break;
case SelectXYZ.z:
vector3.x = vector3.y = 0.0f;
break;
case SelectXYZ.None:
vector3.x = vector3.y = vector3.z = 0.0f;
break;
}
transform.LookAt(cameraLookAt.transform.position - vector3);
}
public enum SelectXYZ
{
x,
y,
z,
None
}
}
相机围绕着物体旋转观察物体脚本
using UnityEngine;
public class Ui3DCamera : MonoBehaviour
{
public Transform target;
public Transform pivot;
public Vector3 pivotOffset = Vector3.zero;
public float distance = 10.0f;
public float minDistance = 2f;
public float maxDistance = 15f;
public float zoomSpeed = 1f;
public float xSpeed = 250.0f;
public float ySpeed = 250.0f;
public float xMinLimit = -30f;
public float xMaxLimit = 30f;
public float yMinLimit = -90f;
public float yMaxLimit = 90f;
public bool allowYTilt = true;
public float targetDistance;
private float x = 0.0f;
private float y = 0.0f;
private float targetX = 0.0f;
private float targetY = 0.0f;
private float xVelocity = 1f;
private float yVelocity = 1f;
private float zoomVelocity = 1f;
internal float scroll;
private void Start()
{
var angles = transform.eulerAngles;
targetX = x = angles.x;
x = Mathf.Clamp(angles.x, xMinLimit, xMaxLimit);
targetY = y;
y = Mathf.Clamp(angles.y, yMinLimit, yMaxLimit);
targetDistance = distance;
}
private void LateUpdate()
{
if (!pivot) return;
scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll > 0.0f) targetDistance -= zoomSpeed;
else if (scroll < 0.0f)
targetDistance += zoomSpeed;
targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);
if (Input.GetMouseButton(0))
{
targetX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
if (allowYTilt)
{
targetY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
targetY = Mathf.Clamp(targetY, yMinLimit, yMaxLimit);
targetX = Mathf.Clamp(targetX, xMinLimit, xMaxLimit);
}
}
x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f);
y = allowYTilt ? Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f) : targetY;
distance = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0.5f);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + pivot.position + pivotOffset;
transform.rotation = rotation;
transform.position = position;
}
}
物体自旋转脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Turn_Move : MonoBehaviour {
public float TurnX;
public float TurnY;
public float TurnZ;
public float MoveX;
public float MoveY;
public float MoveZ;
public bool World;
void Start () {
}
void Update () {
if (World == true) {
transform.Rotate(TurnX * Time.deltaTime,TurnY * Time.deltaTime,TurnZ * Time.deltaTime, Space.World);
transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.World);
}else{
transform.Rotate(TurnX * Time.deltaTime,TurnY * Time.deltaTime,TurnZ * Time.deltaTime, Space.Self);
transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.Self);
}
}
}