創作內容

0 GP

滑鼠+360度選轉

作者:麋瑀│2016-09-03 21:27:36│巴幣:0│人氣:177
using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

public Transform target;//主角
public float x;
public float y;
public float xSpeed = 50;//X靈敏度
public float ySpeed = 50;//Y靈敏度
public float distence = 2;//當前攝影機與主角的距離

public Camera MainCamera;//攝影機
public GameObject MainPerson;//主角
private int RotationSpeed;
private int ScaleSpeed;
private int MouseLeftKey = 0;
private int MouseRightKey = 1;
private float MinScaledistance = 30f;
private float MaxScaledistance = 80f;

private Quaternion rotationEuler;
private Vector3 cameraPosition;
// Use this for initialization
void Start () {
RotationSpeed = 50;
ScaleSpeed = 2;
MainCamera.transform.LookAt(MainPerson.transform.position);
}
// Update is called once per frame
void Update () {
}
void LateUpdate()
{
MouseLeftFunction();
MouseRightFunction();
MouseCenterFunction();  
}
void MouseLeftFunction()
{
//滑鼠左键
if (Input.GetMouseButton(MouseLeftKey))
{
}
}
void MouseRightFunction()
{
//滑鼠右键
if (Input.GetMouseButton(MouseRightKey))
{
//讀取滑鼠的X,Y軸移動訊息
x+= Input.GetAxis("Mouse X")*xSpeed*Time.deltaTime;
y-= Input.GetAxis("Mouse Y")*ySpeed*Time.deltaTime;

//保證X在360度以內
if(x>360)
{
x-=360;
}
else if(x<0)
{
x+=360;
}
}
}
void MouseCenterFunction()
{
//滑鼠中間滾輪向前滾動
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
float CurrentDistance = MainCamera.fieldOfView;
CurrentDistance += ScaleSpeed;
if (CurrentDistance > MaxScaledistance)
MainCamera.fieldOfView = MaxScaledistance;
else
MainCamera.fieldOfView = CurrentDistance;
}
//滑鼠中間滾輪向後滾動
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
float CurrentDistance = MainCamera.fieldOfView;
CurrentDistance -= ScaleSpeed;
if (CurrentDistance < MinScaledistance)
MainCamera.fieldOfView = MinScaledistance;
else
MainCamera.fieldOfView = CurrentDistance;
}
//運算攝影機座標、旋轉
rotationEuler = Quaternion.Euler (y,x,0);
cameraPosition = rotationEuler * new Vector3 (0, 1, -distence) + target.position;

//應用
transform.rotation = rotationEuler;
transform.position = cameraPosition;
}
}
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=3311447
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

喜歡★k940714675 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:自製遊戲公會成員報到單... 後一篇:按鍵(動畫)+重力+跳...

追蹤私訊切換新版閱覽

作品資料夾

dhreekingdon幸運看見的你
給你一顆紅心~讓你能保有一整天的好心情~祝你有個愉快的一天喲(<ゝω・)~❤看更多我要大聲說昨天21:08


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】