首页 > 其他分享 >unity3D实现镜头拉近拉远及视角旋转

unity3D实现镜头拉近拉远及视角旋转

时间:2023-01-05 13:40:13浏览次数:46  
标签:unity3D 拉远 拉近 float transform position rotation public target


镜头拉近拉远的代码(无限拉远拉近)

 

代码如下:

 

if( Input.GetAxis("Mouse ScrollWheel") != 0 )
{
this.gameObject.transform.Translate(new Vector3(0,0,Input.GetAxis("Mouse ScrollWheel")*Time.deltaTime*500));
}

 

上述代码放在Update ()函数中,其中gameObject是摄像机或者物体对象,500是可以调节的参数。方法非常简单。

 

 

上述方法虽然能实现,但是太简单下面来个完整代码如下(只要把这个脚本绑定到相机上就OK);

实现:右键转动视角和镜头拉伸(拉动有范围)

 

/// <summary>
/// Mouse orbit.
/// This script use to control a main camera
/// </summary>

using UnityEngine;
using System.Collections;

public class MouseOrbit : MonoBehaviour {

[HideInInspector]
public GameObject target; //a target look at
public float xSpeed; //speed pan x
public float ySpeed; //speed pan y
public float yMinLimit; //y min limit
public float yMaxLimit; //y max limit

public float scrollSpeed; //scroll speed
public float zoomMin; //zoom min
public float zoomMax; //zoom max

//Private variable
private float distance;
private float distanceLerp;
private Vector3 position;
private bool isActivated;
private float x;
private float y;
private bool setupCamera;

// Use this for initialization

void Start () {


//Warning when not found target
if(target == null)
{
target = GameObject.FindGameObjectWithTag("Player");

if(target == null)
{
Debug.LogWarning("Don't found player tag please change player tag to Player");
}
}


//Setup Pos
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;

CalDistance();
}



void LateUpdate () {

ScrollMouse();
RotateCamera();

}

//Roate camera method
void RotateCamera()
{
if (Input.GetMouseButtonDown(1)){

isActivated = true;

}

// if mouse button is let UP then stop rotating camera
if (Input.GetMouseButtonUp(1))
{
isActivated = false;
}



if (target && isActivated) {

y -= Input.GetAxis("Mouse Y") * ySpeed;

x += Input.GetAxis("Mouse X") * xSpeed;



y = ClampAngle(y, yMinLimit, yMaxLimit);


Quaternion rotation = Quaternion.Euler(y, x, 0);

Vector3 calPos = new Vector3(0, 0, -distanceLerp);

position = rotation * calPos + target.transform.position;

transform.rotation = rotation;

transform.position = position;


} else
{
Quaternion rotation = Quaternion.Euler(y, x, 0);

Vector3 calPos = new Vector3(0, 0, -distanceLerp);

position = rotation * calPos + target.transform.position;

transform.rotation = rotation;

transform.position = position;
}
}

//Calculate Distance Method
void CalDistance()
{
distance = zoomMax;
distanceLerp = distance;
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 calPos = new Vector3(0, 0, -distanceLerp);
position = rotation * calPos + target.transform.position;
transform.rotation = rotation;
transform.position = position;
}

//Scroll Mouse Method
void ScrollMouse()
{
distanceLerp = Mathf.Lerp(distanceLerp,distance,Time.deltaTime * 5);
if (Input.GetAxis("Mouse ScrollWheel") != 0 && !GUI_Menu.instance.CheckHoverItemShop() && !GUI_Menu.instance.CheckHoverSkillWindow())
{
// get the distance between camera and target

distance = Vector3.Distance (transform.position , target.transform.position);

distance = ScrollLimit(distance - Input.GetAxis("Mouse ScrollWheel")*scrollSpeed, zoomMin, zoomMax);

}
}

//Scroll Limit Method
float ScrollLimit(float dist, float min, float max)
{
if (dist < min)

dist= min;

if (dist > max)

dist= max;

return dist;
}


//Clamp Angle Method
float ClampAngle(float angle,float min,float max)
{
if(angle < -360)
angle += 360;
if(angle > 360)
angle -= 360;
return Mathf.Clamp(angle,min,max);
}
}

 

上面的代码只能限定上下的视角,所以我改进了一下,下面的代码可以进行左右和上下视角的范围限定。如下:

/// <summary>
/// Mouse orbit.
/// This script use to control a main camera
/// </summary>

using UnityEngine;
using System.Collections;

public class MouseOrbit : MonoBehaviour {

//[HideInInspector]
public GameObject target; //a target look at
public float xSpeed; //speed pan x
public float ySpeed; //speed pan y
public float yMinLimit; //y min limit
public float yMaxLimit; //y max limit

public float XMinLimit; //X轴的限制
public float XMaxLimit;

public float scrollSpeed; //scroll speed
public float zoomMin; //zoom min
public float zoomMax; //zoom max

//Private variable
private float distance;
private float distanceLerp;
private Vector3 position;
private bool isActivated;
private float x;
private float y;
private bool setupCamera;

// Use this for initialization

void Start () {


//Warning when not found target
if(target == null)
{
//target = GameObject.FindGameObjectWithTag("Player");

if(target == null)
{
Debug.LogWarning("Don't found player tag please change player tag to Player");
}
}


//Setup Pos
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;

CalDistance();
}



void LateUpdate () {

ScrollMouse();
RotateCamera();
if (x < XMinLimit)
{
x = XMinLimit;
}

if (x > XMaxLimit)
{
x = XMaxLimit;
}
}

//Roate camera method
void RotateCamera()
{
if (Input.GetMouseButtonDown(1)){

isActivated = true;

}

// if mouse button is let UP then stop rotating camera
if (Input.GetMouseButtonUp(1))
{
isActivated = false;
}



if (target && isActivated) {

y -= Input.GetAxis("Mouse Y") * ySpeed;

x += Input.GetAxis("Mouse X") * xSpeed;


y = ClampAngle(y, yMinLimit, yMaxLimit);



Quaternion rotation = Quaternion.Euler(y, x, 0);

Vector3 calPos = new Vector3(0, 0, -distanceLerp);

position = rotation * calPos + target.transform.position;

transform.rotation = rotation;

transform.position = position;


} else
{
Quaternion rotation = Quaternion.Euler(y, x, 0);

Vector3 calPos = new Vector3(0, 0, -distanceLerp);

position = rotation * calPos + target.transform.position;

transform.rotation = rotation;

transform.position = position;
}
}

//Calculate Distance Method
void CalDistance()
{
distance = zoomMax;
distanceLerp = distance;
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 calPos = new Vector3(0, 0, -distanceLerp);
position = rotation * calPos + target.transform.position;
transform.rotation = rotation;
transform.position = position;
}

//Scroll Mouse Method
void ScrollMouse()
{
distanceLerp = Mathf.Lerp(distanceLerp,distance,Time.deltaTime * 5);
if (Input.GetAxis("Mouse ScrollWheel") != 0 /*&& !GUI_Menu.instance.CheckHoverItemShop() && !GUI_Menu.instance.CheckHoverSkillWindow()*/)
{
// get the distance between camera and target

distance = Vector3.Distance (transform.position , target.transform.position);

distance = ScrollLimit(distance - Input.GetAxis("Mouse ScrollWheel")*scrollSpeed, zoomMin, zoomMax);

}
}

//Scroll Limit Method
float ScrollLimit(float dist, float min, float max)
{
if (dist < min)

dist= min;

if (dist > max)

dist= max;

return dist;
}


//Clamp Angle Method
//float ClampAngle(float angle,float min,float max)
//{
// if(angle < -360)
// angle += 360;
// if(angle > 360)
// angle -= 360;
// return Mathf.Clamp(angle,min,max);
//}

float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}

 

标签:unity3D,拉远,拉近,float,transform,position,rotation,public,target
From: https://blog.51cto.com/u_8378185/5990795

相关文章

  • Unity3D之数据持久化储存
        首先我们来看两段Unity3D中实现数据读写的简单代码吧://保存数据PlayerPrefs.SetString("Name",mName);PlayerPrefs.SetInt("Age",mAge);PlayerPref......
  • Unity3D常用函数说明
    Unity3D中所有控制脚本的基类MonoBehaviour有一些虚函数用于绘制中事件的回调,也可以直接理解为事件函数,例如大家都很清楚的Start,Update等函数,以下做个总结。 Awake当前控......
  • Unity3D中Resources动态加载NGUI图片
    在NGUI中有些图片我需要动态进行变更或者加载,怎么办?首先在项目中创建一个Resources目录,接着把需要的图片放在这里面,可以有子文件夹么?当然可以,文件结构很重要哦~NGUI加载图片......
  • Unity3D之sprite动画(Animation)的制作
    实例说明:忍者跑酷的player动画制作。。。这些都是用Sprite做的动画。。。在prioject面板里的一组sprite里面点击,之后看属性面板的SpriteEditor对这组Sprite进行编辑。。。......
  • unity3d相机跟随物体平滑移动(C#代码)
    usingUnityEngine;usingSystem.Collections;publicclassCameraFollow:MonoBehaviour{publicTransformtarget;privateVector3wantedPosition......
  • Unity3d碰撞器与触发器的区别详解(rigidbody移动和charactercontroller移动区别)
    Rigidbody或者charactercontroller移动才可以用collider的碰撞。正文:要产生碰撞必须为游戏对象添加刚体(Rigidbody)和碰撞器,刚体可以让物体在物理影响下运动。碰撞体是物理......
  • unity3d脚本之鸡零狗碎
    这里记录的是unity3d开发中用到的鸡零狗碎的代码片段。1、键盘方向键移动对象publicclassKeyboardMove:MonoBehaviour{privatefloatspeed=5f;voidFixedUpdate......
  • unity3d,android平台下,高德地图定位,定位模式设定
    接上一篇,有时候不是想偷懒,实在是不会用androidstudio再次打包啊。高德地图定位androidsdk中,设置定位模式,java代码如下://设置定位模式为高精度模式,Battery_Saving为低功耗......
  • unity3d调用jar类中的方法和属性
    这里用的unity3d的版本是5.3.2在eclipse里新建一个java工程,并导出成jar。packagecom.innyo.androidplugin;publicclassapTest{publicapTest(StringinString){t......
  • unity3d,android平台下,高德地图搜索附近
    今天把高德地图androidsdk搜索附近的功能搞定了。和定位一样,引入jar,AndroidManifest.xml和定位的一样,没有需要修改的地方。改的多的是Java插件。原java示例代码中,query=n......