纳金网

标题: unity3D--如何让摄像机跟随你的角色 [打印本页]

作者: yeu1233    时间: 2012-8-21 18:52
标题: unity3D--如何让摄像机跟随你的角色
场景中建立一个角色,这里我用一个方块代替,方块命名为Player,
//用来获取一个角色对象
public GameObject target;


原先MouseLook代码我不做修改
Update ()里初始化
//这里的Pllayer是我获取的那个对象
target = GameObject.Find("layer");

下面贴出全部代码
using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {

public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;

public float minimumX = -360F;
public float maximumX = 360F;

public float minimumY = -85F;
public float maximumY = 4F;

public float rotationY = 0F;

public GameObject target;
void Update ()
{
target = GameObject.Find("layer");
if(theDistance>0)
theDistance = 0;
if(theDistance < MaxDistance)
theDistance = MaxDistance;
if(Input.GetMouseButton(1))
{
transform.position = target.transform.position;
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
SetDistance();
}
else
{
transform.position = target.transform.position;
SetDistance();
}
}

void Start ()
{
if (rigidbody)
{
rigidbody.freezeRotation = ***e;
transform.position = target.transform.position;
}
}

//设置相机与人物之间的距离
void SetDistance()
{
transform.Translate(Vector3.forward * theDistance);
}
}
作者: 叮铃铃    时间: 2012-8-21 19:11





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5