Unity3D摄像机跟随人物

本篇文章和大家介绍的是摄像机跟随人物, 这里的视角主要是从人物的背后跟随的。

首先新建一个C#脚本,命名为MyFollow,然后把以下代码粘贴进去,保存:
 
 
AخA
 
1
using UnityEngine;  
2
using System.Collections;  
3
public class MyFollow : MonoBehaviour  
4
  
5
    public float distanceAway = 5;          // distance from the back of the craft  
6
    public float distanceUp = 2;            // distance above the craft  
7
    public float smooth = 3;                // how smooth the camera movement is  
8
    private GameObject hovercraft;      // to store the hovercraft  
9
    private Vector3 targetPosition;     // the position the camera is trying to be in  
10
    Transform follow;  
11
    void Start()  
12
        follow = GameObject.FindWithTag ("Player").transform;     
13
      
14
    void LateUpdate ()  
15
      
16
        // setting the target position to be the correct offset from the hovercraft  
17
        targetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;  
18
        // making a smooth transition between it's current position and the position it wants to be in  
19
        transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);  
20
        // make sure the camera is looking the right way!  
21
        transform.LookAt(follow);  
22
      
23
  

接着把上面的这个脚本挂载到摄像机上。

最后把你想跟随的人物的tag设为Player。

运行游戏后,摄像机就可以从人物背后跟随人物了。


nainaitea.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值