码迷,mamicode.com
首页 > 其他好文 > 详细

鼠标控制人物转向

时间:2015-05-21 08:57:50      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

void Start () {
//gameObject.transform.position = new Vector3();
MouseScrollSpeed = 10;
MouseRotateSpeed = 2;
PlayerTransform = GameObject.FindGameObjectWithTag("Player").transform;
DiffPostion = this.transform.position - PlayerTransform.position;
DistanceHeroCamara = DiffPostion.magnitude;
}

// Update is called once per frame
void Update () {
//mouse scroll
DistanceHeroCamara -= Input.GetAxis("Mouse ScrollWheel")*MouseScrollSpeed;

DistanceHeroCamara = Mathf.Clamp(DistanceHeroCamara, 1f, 20f);
// Debug.Log(DistanceHeroCamara);
DiffPostion=DiffPostion.normalized * DistanceHeroCamara;
this.transform.position = DiffPostion + GameObject.FindGameObjectWithTag("Player").transform.position;

//rotate camare and hero
if (Input.GetMouseButtonDown(0) && UICamera.hoveredObject==null)
IsRotating = true;
if (Input.GetMouseButtonUp(0))
IsRotating = false;

float angel = MouseRotateSpeed * Input.GetAxis("Mouse X");
if(IsRotating)
{
this.transform.RotateAround(PlayerTransform.position, Vector3.up, angel);
//PlayerTransform.RotateAround(PlayerTransform.position, Vector3.up, angel);
if (PlayerTransform.gameObject.name == "jiansheng(Clone)")
{
PlayerTransform.forward = new Vector3(-this.transform.forward.x, PlayerTransform.forward.y, -this.transform.forward.z);
}
else
PlayerTransform.forward = new Vector3(this.transform.forward.x, PlayerTransform.forward.y, this.transform.forward.z);

DiffPostion = this.transform.position - PlayerTransform.position;
}

 

//rotate camare and hero do not rotate
if (Input.GetMouseButtonDown(1) && UICamera.hoveredObject == null)
IsRotating2 = true;
if (Input.GetMouseButtonUp(1))
IsRotating2 = false;
if (IsRotating2)
{

Vector3 LastPos = transform.position;
Quaternion LastAngel = transform.rotation;
this.transform.RotateAround(PlayerTransform.position, Vector3.up, MouseRotateSpeed * Input.GetAxis("Mouse X"));
this.transform.RotateAround(PlayerTransform.position, transform.right, -MouseRotateSpeed * Input.GetAxis("Mouse Y"));

float x = transform.eulerAngles.x;
//print(x);
if(x>80 || x<0)
{
transform.position = LastPos;
transform.rotation = LastAngel;
}

DiffPostion = this.transform.position - PlayerTransform.position;
}

}

鼠标控制人物转向

标签:

原文地址:http://www.cnblogs.com/notlate/p/4518759.html

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