码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3D摄像机跟随人物

时间:2015-06-05 12:22:56      阅读:2030      评论:0      收藏:0      [点我收藏+]

标签:unity3d   人物   脚本   游戏   tag   

这里的镜头主要是从人物的背后跟随的。

首先新建一个C#脚本,命名为MyFollow,然后把以下代码粘贴进去,保存:

using UnityEngine;
using System.Collections;

public class MyFollow : MonoBehaviour
{
	public float distanceAway = 5;			// distance from the back of the craft
	public float distanceUp = 2;			// distance above the craft
	public float smooth = 3;				// how smooth the camera movement is
	
	private GameObject hovercraft;		// to store the hovercraft
	private Vector3 targetPosition;		// the position the camera is trying to be in
	
	Transform follow;
	
	void Start(){
		follow = GameObject.FindWithTag ("Player").transform;	
	}
	
	void LateUpdate ()
	{
		// setting the target position to be the correct offset from the hovercraft
		targetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;
		
		// making a smooth transition between it's current position and the position it wants to be in
		transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);
		
		// make sure the camera is looking the right way!
		transform.LookAt(follow);
	}
}

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

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

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

Unity3D摄像机跟随人物

标签:unity3d   人物   脚本   游戏   tag   

原文地址:http://blog.csdn.net/liujan511536/article/details/46374431

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!