查看: 1144|回复: 1
打印 上一主题 下一主题

按住右键旋转观看,放开后恢复默认位置(转)

[复制链接]

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-11-7 08:57:22 |只看该作者 |倒序浏览
using UnityEngine;
using System.Collections;public class HackAndSlashCamera : MonoBehaviour {
public Transform target;
public float walkDistance;
public float&nbsp***nDisatance;
public float height;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;private Transform _myTransform;
private float _x;
private float _y;
private bool _camButtonDown = false;
void Awake() {
_myTransform = transform;
}// Use this for initialization
void Start () {
if(target == null)
Debug.LogWarning(“We do not have a target for the camera”);
else {
CameraSetUp();
}
}void Update() {
if(Input.GetMouseButtonDown(1)) { //Use the Input Manager to make this user selectable button
_camButtonDown = ***e;
}
if(Input.GetMouseButtonUp(1)) {
_camButtonDown = false;
}
}//this function is called after all of the Update functions are done.
void LateUpdate() {
// _myTransform.position = new Vector3(target.position.x,target.position.y + height,target.position.z – walkDistance);
// _myTransform.LookAt(target);
if(target != null) {
if(_camButtonDown) { //Use the Input Manager to make this user selectable button
_x += Input.GetAxis(“Mouse X”) * xSpeed * 0.02f;
_y -= Input.GetAxis(“Mouse Y”) * ySpeed * 0.02f;// y = ClampAngle(y, yMinLimit, yMaxLimit);Quaternion rotation = Quaternion.Euler(_y, _x, 0);Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position;_myTransform.rotation = rotation;
_myTransform.position = position;
}
else{
// _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z – walkDistance);
// _myTransform.LookAt(target);
_x = 0; //reset the x value
_y = 0; //reset the y value//Calculate the current ratation angles
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;float currentRotationAngle = _myTransform.eulerAngles.y;
float currentHeight = _myTransform.position.y;//Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);//Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);//Convert the angle into a rotation
Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);//Set the position of the camera on the x-z plane to:
//distance meters behind the target
_myTransform.position = target.position;
_myTransform.position -= currentRotation * Vector3.forward * walkDistance;//Set the height of the camera
_myTransform.position = new Vector3(_myTransform.position.x, currentHeight, _myTransform.position.z);//Always look at the target
_myTransform.LookAt(target);
}
}
}//set the camera to a default position behind the player and facing them
public void CameraSetUp() {
_myTransform.position = new Vector3(target.position.x,target.position.y + height,target.position.z – walkDistance);
_myTransform.LookAt(target);
}
}
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-11-7 08:57:42 |只看该作者
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-1-31 10:51 , Processed in 0.063204 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部