【Unity自制手册】Unity—Camera相机跟随的方法大全

在这里插入图片描述


👨‍💻个人主页:@元宇宙-秩沅

👨‍💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

👨‍💻 本文由 秩沅 原创

👨‍💻 收录于专栏:Unity基础实战

🅰️



文章目录

    • 🅰️
    • 前言
    • 🎶(==1==) 挂载于父对象上进行跟随
    • 🎶(==2==)位置定点跟随,滑轮改变视野
    • 🎶(==3==) 距离差值进行跟随
    • 🎶(==4==) LookAt上帝视角的跟随
    • 🎶(==5==)相机的第一/三人称跟随(添加了跟随点)
    • 🎶(==6==)相机 Lerp差值跟随
    • 🅰️


前言


🎶(1 挂载于父对象上进行跟随



🎶(2位置定点跟随,滑轮改变视野


😶‍🌫️效果:
摄像机需要实现跟随,车同步移动,旋转。并且滑动鼠标滑轮可以调节与车辆之间的摄影距离。

在这里插入图片描述


public class CameraFllow : MonoBehaviour
{//目标物体public Transform target;//鼠标滑轮的速度public float ScrollSpeed = 4f;//Y轴差距参数public float Ydictance = 0f; public float  Ymin = 0f;public float  Ymax  = 4f;//Z轴差距参数public float Zdictance = 4f;public float Zmin = 4f;public float Zmax = 8f;//相机看向的角度 和最終位置public float angle = -25 ;public Vector3 lookPosition;void LateUpdate(){//Z轴和Y轴的距离和鼠标滑轮联系Ydictance += Input.GetAxis("Mouse ScrollWheel") * ScrollSpeed;Zdictance += Input.GetAxis("Mouse ScrollWheel") * ScrollSpeed;//設置Y軸和x轴的滚轮滑动范围Ydictance = Mathf.Clamp(Ydictance , Ymin ,Ymax );Zdictance = Mathf.Clamp(Zdictance , Zmin, Zmax );//确定好角度,四元数 * 三维向量 = 三维向量lookPosition = Quaternion.AngleAxis(angle, target .right) * -target.forward ;//更新位置transform.position = target.position + Vector3.up * Ydictance - lookPosition  * Zdictance  ;//更新角度transform.rotation = Quaternion.LookRotation(lookPosition);}
}

🎶(3 距离差值进行跟随


在这里插入图片描述

public class CameraMove : MonoBehaviour
{public Transform target; //跟随的目标物体private Vector3 offset;  //位置偏移差void Start(){offset = transform.localPosition - target.transform.localPosition;}private void FixedUpdate(){if (target){transform.rotation = target.rotation ;transform.rotation *= Quaternion.AngleAxis(-15, Vector3.left);transform.position = target.transform.localPosition + offset;}}}

🎶(4 LookAt上帝视角的跟随


在这里插入图片描述

public class CameraMove : MonoBehaviour
{public Transform target; //跟随的目标物体private Vector3 offset;  //位置偏移差void Start(){offset = transform.localPosition - target.transform.localPosition;}private void FixedUpdate(){if (target){           transform.rotation = target.rotation ;           transform.position = target.transform.localPosition + offset;transform.LookAt(target.position +Vector3 .up*3);}}}

🎶(5相机的第一/三人称跟随(添加了跟随点)


  • 为了实现相机和人物的镜头旋转保持一致,(达到相机作为子对象的效果)
  • 所以只需要再父对象中添加一个跟随点作为其子对象
    在这里插入图片描述
    在这里插入图片描述
public class CameraMove : MonoBehaviour
{public Transform target; //跟随的目标物体private Vector3 offset;  //位置偏移差void Start(){offset = transform.position - target.GetChild(0).position;//target的第一个子对象是相机的跟随点}private void FixedUpdate(){if (target){           transform.rotation = target.GetChild(0).rotation ;           transform.position = target.GetChild(0).position+ offset;transform.LookAt(target.position + Vector3 .up*2.5f);///transform.rotation = target.rotation;}}}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:       ______________
//___________功能:  玩家的移动
//___________创建者:___秩沅____
//_____________________________________
//-------------------------------------
public class PlayerMove : MonoBehaviour
{private float vertical;private float horizontal;private float mousePosition;private CharacterController player; //角色控制器private Vector3 moveDerictor;       //移动的方向public  float  velocity = 2f;       //移动的速度public  float roVelocity = 10f;private Animator playerAnimatior;private void Awake(){player = GetComponent<CharacterController>();playerAnimatior = GetComponent<Animator>();}private void FixedUpdate(){vertical   =  Input.GetAxis("Vertical") ;horizontal =  - Input.GetAxis("Horizontal") ;mousePosition = Input.GetAxis("Mouse X");//旋转transform.localRotation *= Quaternion.Euler(0, mousePosition * roVelocity, 0);if (vertical != 0 ||horizontal != 0){        //移动playerAnimatior.SetFloat("SpeedWS", (int)vertical);playerAnimatior.SetFloat("SpeedAD", (int)horizontal);moveDerictor = new Vector3(vertical, 0, horizontal);print(moveDerictor.normalized);/// moveDerictor = moveDerictor.normalized;   //将方向变成单位向量//transform.position= transform.position + moveDerictor.normalized*Time .deltaTime ;player.SimpleMove(transform.forward * vertical );player.SimpleMove(transform.right * -horizontal);//GetComponent<Rigidbody>().MovePosition( transform.localPosition + moveDerictor * velocity * Time.deltaTime); //速度*方向 = 向量//此时物体并非跟着自己的旋转方向进行移动而是根据自身位置进行改变//(白话:无法变成FPS的第一视角进行当前视角当前前进)       }}private void MouseRotation(){}}

🎶(6相机 Lerp差值跟随


在这里插入图片描述

     transform.position = Vector3.Lerp(transform.position, target[ChooseIndex].position, Time.deltaTime * speed);transform.LookAt(targetOb );

🅰️


⭐【Unityc#专题篇】之c#进阶篇】

⭐【Unityc#专题篇】之c#核心篇】

⭐【Unityc#专题篇】之c#基础篇】

⭐【Unity-c#专题篇】之c#入门篇】

【Unityc#专题篇】—进阶章题单实践练习

⭐【Unityc#专题篇】—基础章题单实践练习

【Unityc#专题篇】—核心章题单实践练习


你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!


在这里插入图片描述


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/706101.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

搭建freqtrade量化交易机器人

本文采用python量化机器人框架 freqtrade 开始操作&#xff01; freqtrade官方文档 官方文档内容过多&#xff0c;请先跟随本文入门阅读&#xff0c;后续深入学习可参考官方文档&#xff5e; 1. 准备云服务器 docker 环境 这里以云服务器选择 ubuntu 系统开始&#xff0c;先…

微信小程序的医院体检预约管理系统springboot+uniapp+python

本系统设计的目的是建立一个简化信息管理工作、便于操作的体检导引平台。共有以下四个模块&#xff1a; uni-app框架&#xff1a;使用Vue.js开发跨平台应用的前端框架&#xff0c;编写一套代码&#xff0c;可编译到Android、小程序等平台。 语言&#xff1a;pythonjavanode.js…

VBA来创建一个新的 Excel 文件

前言 其他的宏指令执行的前提条件是创建一个新的xlsx文件,来存储操作完成后的结果.否则会因为缺少操作对象,出现1004错误. Sub CreateNewFile()Dim xlApp As ObjectDim xlWB As Object 创建一个新的 Excel 应用程序对象Set xlApp CreateObject("Excel.Application")…

docker学习快速入门

目录 Linux下安装docker配置阿里云镜像加速docker命令部署安装Tomcat、ES容器数据卷DockerFiledocker网络制作tomcat镜像Redis集群部署SpringBoot微服务打包docker镜像拓展 什么是Docker Docker是内核级别的虚拟化&#xff0c;可以在一个物理机上可以运行很多的容器实例。服务…

Unity使用PlayableAPI 动态播放动画

1.初始化animator&#xff0c;创建Playable图&#xff0c;创建动画Playable private void InitAnimator(GameObject headGo) {if (headGo){_headAnimator headGo.GetComponent<Animator>();if (_headAnimator){_headAnimator.cullingMode AnimatorCullingMode.AlwaysA…

【面试】找工作历程

简单介绍一下自己&#xff1a;本科双非一本&#xff0c;22年毕业&#xff0c;工作一段时间到24年1月份辞职&#xff0c;怎么说实习加正式&#xff0c;工作了大概两年&#xff0c;年后准备换工作&#xff0c;目前IP上海。 2024.2.26 第一天正式投简历投了boss的上限&#xff0c;…

Sentinel 动态规则扩展

一、规则 Sentinel 的理念是开发者只需要关注资源的定义&#xff0c;当资源定义成功后可以动态增加各种流控降级规则。Sentinel 提供两种方式修改规则&#xff1a; 通过 API 直接修改 (loadRules)通过 DataSource 适配不同数据源修改 手动通过 API 修改比较直观&#xff0c;…

主机字节序与网络字节序

大端序和小端序 大端序&#xff08;Big Endian&#xff09;和小端序&#xff08;Little Endian&#xff09;是两种计算机存储数据的方式。 大端序指的是将数据的高位字节存储在内存的低地址处&#xff0c;而将低位字节存储在内存的高地址处。这类似于我们阅读多位数时从左往右…

YOLOv6代码解读[05] yolov6/core/engine.py文件解读

#!/usr/bin/env python3 # -*- coding:utf-8 -*- from ast import Pass import os import os.path as osp import time from copy import deepcopy from tqdm import tqdm import cv2 import numpy as np import mathimport torch from torch.cuda

新版vscode remote ssh不兼容老系统 (waiting for server log)

参考知乎-萌萌哒赫萝​ 最近vscode发布了1.86版本&#xff0c;该版本中&#xff0c;更新了对glibc的要求( ≥ \geq ≥ 2.28)&#xff0c;导致各种旧版本的linux发行版&#xff08;如centos 7&#xff09;都无法用remote-ssh来连接了&#xff0c;会一直控制台报错waiting for s…

迁移学习 领域自适应

迁移学习 什么是迁移学习 迁移学习是机器学习领域用于标记数据难获取这一基础问题的重要手段&#xff0c; 将训练好的内容应用到新的任务上被称为迁移学习。 由于这个过程发生在两个领域间&#xff0c;已有的知识和数据也就是被迁移的对象被称为源域&#xff0c;被赋予经验…

防火墙的内容安全

目录 1. 内容安全 1.1 IAE引擎 DPI---深度包检测技术 DFI---深度流检测技术 结论(优缺点)&#xff1a; 1.2 入侵防御&#xff08;检测&#xff09;(IPS) IPS的优势: 入侵检测的方法: 入侵检测的流程 签名 查看预定义签名的内容 新建自定义签名 入侵防御的检测…

面试题解答

题目 为管理业务培训信息&#xff0c;现需建立3个表&#xff1a; 表S(S#,SN,SD,SA)S#,SN,SD,SA分别代表学号&#xff0c;学员姓名&#xff0c;所属单位&#xff0c;学员年龄、 表C(C#,CN)C#,CN分别代表课程编号&#xff0c;课程名称 表SC(S#,C#,G)S#,C#,G分别代表学号&#xf…

热闹元宵进行中,如何利用VR全景展示民宿品牌形象?

错峰出游闹元宵&#xff0c;元宵节恰逢周末&#xff0c;而且还是春节假期返工之后的首个休息日&#xff0c;不少人都想通过短途度假来缓解“节后综合征”。两位数的特价机票、打折的各种酒店让你实现“旅行自由”&#xff0c;那么如何知道特价酒店服务好不好呢&#xff1f;先别…

Leetcode.901 股票价格跨度

题目信息 LeetoCode地址: . - 力扣&#xff08;LeetCode&#xff09; 题目理解 价格跨度的定义在题目中很明确&#xff0c;就是韭菜持有一只股票且该股票保持连续上涨最大的天数。 直观的想&#xff0c;我们可以保存第一天到当前天的所有股价&#xff0c;并一天一天往前找单…

2-27练习

1、请用fscanf和fprintf实现文件拷贝。 &#xff08;fputc和fgetc&#xff09; #include <stdio.h> int main(int argc, const char *argv[]) {FILE* fp NULL;fp fopen("./z1.txt","r");//用fscanf统计文件大小int count 0;char c; // while(fs…

对于大前端开发来说,转鸿蒙开发究竟是福还是祸?

从铺天盖地的市场消息来看&#xff0c;华为即将面世的鸿蒙NEXT系统已经势不可挡了 想必大家都已经迫不及待地想要进行尝试。 估计大家都有着同样的疑问&#xff1a; 会不会是下一个风口&#xff1f;转鸿蒙应用开发难吗&#xff1f; 会不会是下一个风口&#xff1f; 自从鸿蒙…

江科大stm32 定时器 TIM输出比较--学习笔记

这几天遇到输出比较相关的问题&#xff0c;于是来学习下TIM输出比较部分知识点&#xff01; 输出比较简介 CNT是计数器的值&#xff0c;CCR寄存器是捕获/ 比较寄存器 简单的讲&#xff0c;输出比较就是用来输出PWM波形。 PWM简介 占空比&#xff1a;高电平占一个周期的比例。…

搜索算法(算法竞赛、蓝桥杯)--双向BFS字串变换

1、B站视频链接&#xff1a;B19 双向BFS 字串变换_哔哩哔哩_bilibili 题目链接&#xff1a;[NOIP2002 提高组] 字串变换 - 洛谷 #include <iostream> #include <queue> #include <unordered_map> using namespace std;const int N7; int n; string A,B,a[N]…

opencascade c#例程解析

1.编译 将msvc.bat文件拖入vs2022的x64 native tools&#xff0c;即可 2.about.xaml <Windowxmlns"http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x"http://schemas.microsoft.com/winfx/2006/xaml"x:Class"IE_WPF_WinForms…