一、飞行相机代码展示:
UpDown需要在如下添加A、D键
using System;
using UnityEngine;
public class FlightCamera : MonoBehaviour
{
public Transform flyCamera;
public float translationSpeed = 5f;
public float runTranslationSpeed = 10f;
public float rotationSpeed = 100f;
public float rotationMultiplier = 2f; // 控制旋转速度的倍数
// public float minRotationAngle = -80f; // 最小旋转角度
// public float maxRotationAngle = 80f; // 最大旋转角度
private Vector3 posInit;
private Vector3 rotInit;
private Vector3 flyCameraPosInit;
void Awake()
{
posInit = this.transform.position;
rotInit = this.transform.eulerAngles;
flyCameraPosInit = flyCamera.transform.localEulerAngles;
}
public void StartFlyCamera()
{
this.transform.position = posInit;
this.transform.eulerAngles = rotInit;
flyCamera.transform.localEulerAngles = flyCameraPosInit;
}
void Update()
{
var _translationSpeed = Input.GetKey(KeyCode.LeftShift) ? runTranslationSpeed : translationSpeed;
// 前后移动
float verticalInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * verticalInput * _translationSpeed * Time.deltaTime);
// 上下平移
float upDownInput = Input.GetAxis("UpDown");
transform.Translate(Vector3.up * upDownInput * _translationSpeed * Time.deltaTime);
// 左右旋转
float horizontalInput = Input.GetAxis("Horizontal");
transform.Rotate(Vector3.up, horizontalInput * rotationSpeed * Time.deltaTime);
// 鼠标右键按下并上下滑动控制 X 轴旋转
if (Input.GetMouseButton(1))
{
float mouseY = Input.GetAxis("Mouse Y") * rotationSpeed * rotationMultiplier * Time.deltaTime;
// float newRotationX = Mathf.Clamp(flyCamera.localEulerAngles.x - mouseY, minRotationAngle, maxRotationAngle);
flyCamera.Rotate(-mouseY, 0f, 0f);
}
}
}
二、路径相机(直接播放版):
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class PathFollowing : MonoBehaviour
{
public Transform waypointsParents;
[SerializeField]
private List<Transform> waypoints;
public float speed = 5.0f;
public float minDistanceToWaypoint = 0.1f;
public float rotationSpeed = 5.0f;
private int currentWaypointIndex = 0;
private Vector3 posInit;
private Vector3 rotInit;
void Awake()
{
var waypointsTrans = waypointsParents.GetComponentsInChildren<Transform>();
for (int i = 1; i < waypointsTrans.Length; i++)
{
waypoints.Add(waypointsTrans[i]);
}
posInit = this.transform.position;
rotInit = this.transform.eulerAngles;
}
public void StartPathCamera()
{
this.transform.position = posInit;
this.transform.eulerAngles = rotInit;
currentWaypointIndex = 0;
StopCoroutine(FollowPath());
StartCoroutine(FollowPath());
}
IEnumerator FollowPath()
{
while (currentWaypointIndex < waypoints.Count)
{
Transform targetWaypoint = waypoints[currentWaypointIndex];
while (Vector3.Distance(transform.position, targetWaypoint.position) > minDistanceToWaypoint)
{
Vector3 direction = (targetWaypoint.position - transform.position).normalized;
transform.position += direction * speed * Time.deltaTime;
Quaternion targetRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
yield return null;
}
currentWaypointIndex++;
if (currentWaypointIndex >= waypoints.Count)
{
yield break;
}
}
}
}
三、手机触摸实现控制模型旋转
using UnityEngine;
public class TouchRotate : MonoBehaviour
{
public float rotateSpeed = 1f; // 旋转速度
private Vector2 lastTouchPosition;
void Update()
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
// 获取当前触摸位置
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
// 计算触摸位置的差值,以控制模型旋转
float rotationX = -touchDeltaPosition.y * rotateSpeed;
float rotationY = -touchDeltaPosition.x * rotateSpeed;
// 根据触摸差值旋转模型
transform.Rotate(Vector3.up, rotationY, Space.World);
transform.Rotate(Vector3.right, rotationX, Space.World);
}
}
}
四、鼠标击中某个模型播放视频:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class ScreenRayToVideo : MonoBehaviour
{
public RawImage videoImage;
public RenderTexture renderTexture;
public VideoPlayer videoPlayer;
private void Awake()
{
videoPlayer.loopPointReached += OnVideoEnd;
videoPlayer.prepareCompleted += OnVideoPrepared;
videoPlayer.Prepare();
}
void Update()
{
// 当用户点击鼠标左键时
if (Input.GetMouseButtonDown(0))
{
// 发射一条射线从鼠标点击的屏幕位置
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// 如果射线击中了某个模型
if (Physics.Raycast(ray, out hit))
{
// 检查是否击中的是一个模型
GameObject hitObject = hit.collider.gameObject;
Debug.Log("Clicked on: " + hitObject.name);
// 获取模型上的VideoPlayer组件
VideoPlayer videoPlayer = hitObject.GetComponent<VideoPlayer>();
// 检查VideoPlayer组件是否存在
if (videoPlayer != null)
{
// 获取当前视频播放的时间(以秒为单位)
double currentTime = videoPlayer.time;
PlayVideoFromPath("E:\\Desktop\\录屏.mp4");
Debug.Log("Current video time: " + currentTime + " seconds");
}
}
}
}
void OnVideoEnd(VideoPlayer vp)
{
videoPlayer.Stop();
// playToggle.isOn = false;
// videoImage.enabled = false;
}
void OnVideoPrepared(VideoPlayer vp)
{
renderTexture = new RenderTexture((int)videoPlayer.width, (int)videoPlayer.height, 24);
renderTexture.Create();
videoPlayer.targetTexture = renderTexture;
videoImage.texture = renderTexture;
// videoImage.enabled = true;
}
public void PlayVideoFromPath(string videoPath)
{
videoImage.gameObject.SetActive(true);
videoPlayer.url = videoPath;
videoPlayer.Play();
}
}