相机跟随

几次的相机跟随的练习让我发现我还是需要记录一下不同的相机跟随的实现

相机跟随的移动一般写在LateUpdate里,这样不会造成一些失真现象。

1.在固定位置的相机跟随

public class FollowTarget : MonoBehaviour {

    public Transform character;  //摄像机要跟随的人物
    private Camera mainCamera;//主摄像机

    void Awake()
    {
        mainCamera = Camera.main;
    }

    void FixedUpdate()
    {
        transform.position = character.TransformPoint(new Vector3(0, 4, -5));
        transform.LookAt(character);
    }
}
2.有鼠标滚轮缩进的相机跟随

using UnityEngine;

public class FollowTarget : MonoBehaviour {
	public Transform target;
	Vector3 offset;

	void Update()  
	{
		transform.position = target.position + offset;
		Rotate();
		Scale();
	}


	// Use this for initialization
	void Start()
	{
		offset = transform.position - target.position;
	}

	// Update is called once per frame

	//缩放
	private void Scale()
	{
		float dis = offset.magnitude;
		dis += Input.GetAxis("Mouse ScrollWheel") * 5;
		Debug.Log("dis=" + dis);
		if (dis < 10 || dis > 40)
		{
			return;
		}
		offset = offset.normalized * dis;
	}
	//左右上下移动
	private void Rotate()
	{
		if (Input.GetMouseButton(1))
		{
			Vector3 pos = transform.position;
			Vector3 rot = transform.eulerAngles;

			//围绕原点旋转,也可以将Vector3.zero改为 target.position,就是围绕观察对象旋转
			transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * 10);
			transform.RotateAround(Vector3.zero, Vector3.left, Input.GetAxis("Mouse Y") * 10);
			float x = transform.eulerAngles.x;
			float y = transform.eulerAngles.y;
			Debug.Log("x=" + x);
			Debug.Log("y=" + y);
			//控制移动范围
			if (x < 20 || x > 45 || y < 0 || y > 40)
			{
				transform.position = pos;
				transform.eulerAngles = rot;
			}
			//  更新相对差值
			offset = transform.position - target.position;
		}
	}
}



<think>好的,我现在需要处理用户关于Unity相机跟随功能的查询。首先,用户明确要求实现方法或教程以及代码示例。我需要结合之前提供的引用内容,确保回答准确且有帮助。 首先,回顾用户提供的引用资料。引用1提到了Cinemachine的ClearShot和BlendListCamera,引用3建议使用Cinemachine的Virtual Camera,引用4给出了简单的父子级设置代码,引用2展示了固定偏移的跟随脚本。用户可能想要多种实现方式,包括基础方法和高级的Cinemachine用法。 接下来,我需要整理几种常见的相机跟随方法。首先是最简单的通过父子关系绑定,这在引用4中有示例代码,直接设置相机的父对象为目标物体。这种方法简单但缺乏灵活性,适合初学者或简单场景。 然后是固定偏移跟随,如引用2的FixedCameraFollower脚本,使用目标位置减去偏移量来更新相机位置。这种方法在FixedUpdate中更新,适合物理相关的跟随,能减少抖动。需要解释代码结构和参数作用。 第三种是平滑跟随,使用Lerp或SmoothDamp实现平滑移动。这里需要编写一个示例脚本,可能需要解释插值系数和速度参数的作用,确保用户理解如何调整平滑效果。 接下来是Cinemachine的Virtual Camera方法,引用1和3提到过。这里需要指导用户安装Cinemachine包,创建Virtual Camera,设置Follow和Look At属性。同时可以提到Cinemachine的优点,如自动处理遮挡、多种跟随模式等,适合复杂需求。 用户可能对每种方法的适用场景和优缺点感兴趣,所以需要对比总结。比如,父子关系最简单但不够灵活,固定偏移适合固定视角,平滑跟随适合需要流畅移动的情况,而Cinemachine提供最专业和可定制的解决方案。 最后,生成相关问题时要覆盖不同实现方式的深入问题、性能优化、不同场景应用以及与其他Unity功能的结合,比如动画系统或物理引擎。 需要确保所有代码示例格式正确,使用中文注释,并正确引用提供的资料。同时,按照用户要求,行内数学公式用$...$,但这里可能用不上。检查是否有遗漏的方法或常见问题,比如相机抖动处理、跟随延迟调整等,可能在相关问题中提及。</think>### Unity相机跟随功能实现方法详解 在Unity中实现相机跟随主要有以下五种方法,根据不同的使用场景推荐不同方案: 一、**父子级绑定法**(适合快速原型开发) ```csharp // 引用自示例代码[^4] GameObject player = GameObject.Find("Player"); Camera.main.transform.SetParent(player.transform); Camera.main.transform.localPosition = new Vector3(0, 5, -10); // 设置相对位置 ``` * 优点:实现简单,无需代码 * 缺点:完全同步移动,缺乏平滑过渡 二、**固定偏移跟随法**(适合2D/3D平台游戏) ```csharp // 改编自引用[2]的代码示例 public class CameraFollow : MonoBehaviour { public Transform target; public Vector3 offset = new Vector3(0, 5, -10); void LateUpdate() { transform.position = target.position + offset; } } ``` * 使用$LateUpdate()$确保在目标移动后更新 * 偏移量计算:$\Delta x = x_{target} - x_{camera}$ 三、**平滑跟随法**(适合需要流畅移动的场景) ```csharp public class SmoothFollow : MonoBehaviour { public Transform target; public float smoothTime = 0.3f; private Vector3 velocity = Vector3.zero; void LateUpdate() { Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10)); transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime); } } ``` * 使用$SmoothDamp$函数实现二阶平滑 * 平滑系数公式:$v(t) = v_0 + a \cdot t$ 四、**Cinemachine实现法**(专业级解决方案) 1. 通过Package Manager安装Cinemachine 2. 创建Virtual Camera: ```csharp // 在场景中右键 -> Cinemachine -> Create Virtual Camera ``` 3. 设置参数: - Follow属性绑定玩家Transform - Look At属性设置注视目标 - Body设置跟随模式(推荐使用Framing Transposer) 五、**物理跟随法**(适合物理驱动的场景) ```csharp public class PhysicsFollow : MonoBehaviour { public Transform target; public Rigidbody rb; public float followForce = 10f; void FixedUpdate() { Vector3 desiredPosition = target.position + new Vector3(0, 5, -10); Vector3 positionDelta = desiredPosition - rb.position; rb.AddForce(positionDelta * followForce); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值