Unity实现飞行相机、路径相机、手机触摸旋转模型、鼠标点击模型播放视频

一、飞行相机代码展示:

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();
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

平杨猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值