当前位置:网站首页>[unity3d] camera follow
[unity3d] camera follow
2022-06-28 18:46:00 【little_ fat_ sheep】
1 Preface
The camera follows Camera means always following a particular GameObject , There are the following 2 Kinds of following effect :
- Position follows : The vector that the camera points to the target GameObject is always the same
- Position and attitude follow : The coordinates and orientation of the camera in the coordinate system of the target game object remain unchanged

The simplest way to follow the camera position and attitude is : Drag the camera GameObject under the target GameObject , That is, the camera is the sub object of the target game object , The principle is : The position and posture of the child object always change with the change of the parent object , And remain relatively unchanged . This article does not show this approach , The camera position transformation will be controlled by code , Realize the camera following effect .
2 application
2.1 Position follows
1) The game object
The object of the game Transform The component parameters are as follows :
| Name | Type | Position | Rotation | Scale | Color/Texture |
|---|---|---|---|---|---|
| Plane | Plane | (0, 0, 0) | (0, 0, 0) | (10, 10, 10) | GrassRockyAlbedo |
| Tank | Empty | (0, 0.25, -5) | (0, 0, 0) | (1, 1, 1) | —— |
| Button | Cube | (0, 0, 0) | (0, 0, 0) | (2, 0.5, 2) | #228439FF |
| Top | Cube | (0, 0.5, 0) | (0, 0, 0) | (1, 0.5, 1) | #228439FF |
| Gun | Cylinder | (0, 0, 1.5) | (90, 0, 0) | (0.2, 1, 0.4) | #228439FF |
The hierarchy of the game object is as follows :

2) Script components
Tank.cs
using UnityEngine;
public class Tank : MonoBehaviour {
void Update () {
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
transform.Translate(0, 0, ver * Time.deltaTime * 3);
transform.Rotate(Vector3.up * hor * Time.deltaTime * 120f);
}
}explain :Tank.cs The script component is hung on Tank On the object of the game .
CameraFollow.cs
using UnityEngine;
public class CameraFollow : MonoBehaviour {
private Transform player; // The player
private Vector3 dir; // The direction vector from the initial camera to the target tank
void Start() {
player = GameObject.Find("Tank").transform;
dir = player.position - transform.position;
}
void Update () {
transform.position = player.position - dir;
}
}explain :CameraFollow.cs The script component is hung on Main Camera On the object of the game .
3) Running effect

2.2 Position and attitude follow
The game object is its Transform Component parameters are the same as 2.1 section ,.
1) Script components
Tank.cs Script components are the same as 2.1 section .
CameraFollow.cs
using UnityEngine;
public class CameraFollow : MonoBehaviour {
private Transform player; // The player
private Vector3 dir; // The direction vector from the initial camera to the target tank
void Start() {
player = GameObject.Find("Tank").transform;
dir = player.position - transform.position;
}
void Update () {
transform.position = transformVecter(-dir, player.position, player.right, player.up, player.forward);
transform.rotation = Quaternion.LookRotation(player.position - transform.position);
}
// Seeking for origin Origin , locX, locY, locZ Is the vector in the local coordinate system of the coordinate axis vec The corresponding vector in the world coordinate system
private Vector3 transformVecter(Vector3 vec, Vector3 origin, Vector3 locX, Vector3 locY, Vector3 locZ) {
return vec.x * locX + vec.y * locY + vec.z * locZ + origin;
}
}2) Running effect

边栏推荐
- 中金财富开户安全吗?开过中金财富的讲一下
- PMP怎么补考?补考费用是多少?
- 浅谈软件研发的复杂性与效能提升之道
- 基于管线的混合渲染
- konva系列教程3:自定义图形
- An in-depth analysis of the election mechanism in kubernetes
- 原生实现.NET 5.0+ 自定义日志
- use. NETCORE's own background job, which simply simulates producers and consumers' processing of request response data in and out of the queue
- 几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
- ANR Application Not Responding
猜你喜欢

电子商务盛行,怎么提高商店转换率?

Qt 中 QObjectCleanupHandler 使用总结

PCB线路板布局和布线都有哪些设计要求?

数字化转型的1个目标,3大领域,6大因素和9个环节

PHP使用栈解决迷宫问题

抗兔Dylight 488丨Abbkine通用型免疫荧光(IF)工具箱

curl: (56) Recv failure: Connection reset by peer

新工作第一天

ONEFLOW source code parsing: automatic inference of operator signature

Go, begin, end, for, after, instead of
随机推荐
使用.NetCore自带的后台作业,出入队简单模拟生产者消费者处理请求响应的数据
BioVendor游离轻链(κ和λ)Elisa 试剂盒检测步骤
ANR Application Not Responding
Database Experiment 7 integrity constraints
Operations research note
golang json 序列化、反序列化 字符串反序列化成 map[string]interface{}
堆的概念和代码实现
Shell脚本批量修改文件目录权限
亿信华辰:地产企业数字化转型想要把握时代机遇
解析机器人主持教学的实践发展
leetcode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers(最少的“二进制数“个数)
What are the design requirements for PCB layout and wiring?
C语言指针的一些易错点
获取当前日期0点及23点59时间戳
Go, begin, end, for, after, instead of
Can I open an account today and buy shares today? Is it safe to open an account online?
POI Excel转换工具
模块化操作
牛津大學教授Michael Wooldridge:AI社區近40年如何看待神經網絡
OneFlow源码解析:算子签名的自动推断