2016-02-19 11:08:49 wuyt2008 阅读数 6222

主摄像机设置




背景摄像机设置,其中,Depth最好小于或等于主摄像机的Depth



背景摄像机看到的物体设置



场景整体设置和实际效果


2018-04-26 09:49:32 luoyikun 阅读数 647
using UnityEngine;
using System.Collections;

public class LookAtCam : MonoBehaviour {
    public Transform m_camTrans;
    // Use this for initialization
    void Start () {

    }

    void Update()
    {
        Vector3 relativePos = transform.position - m_camTrans.position;
        Quaternion lookAtRotation = Quaternion.LookRotation(relativePos, Vector3.up);
        this.transform.rotation = Quaternion.Lerp(this.transform.rotation, lookAtRotation,  Time.deltaTime);
    }
}

非插值

public class BEBillboard3D : MonoBehaviour {

        public bool UsePosition = false;
        Camera cam;

        void Start() {
            cam = Camera.main;
        }

        void Update() {

            if(UsePosition) {
                Vector3 vDir = cam.transform.position - transform.position;
                vDir.Normalize();
                transform.rotation = Quaternion.LookRotation(-vDir);
            }
            else {
                transform.rotation = cam.transform.rotation;
            }
        }
    }
2017-06-08 21:41:27 u014805591 阅读数 1565

这里写图片描述

本篇博文提供一个摄像机控制类,给大家参考和交流!

using UnityEngine;
using System.Collections;

public class MouseOrbit : MonoBehaviour {


    /// <summary>
    /// 得到物体的名字
    /// </summary>
    //public string Getobejctname;

    public Transform initPosObject;

    //public Transform targetObject;

    /// <summary>
    /// ---------------限制速度-----------------------
    /// </summary>

    public float xSpeed = 350.0f;
    public float ySpeed = 250.0f;

    /// <summary>
    /// ------------ 限制X和Y的角度-------------------
    /// </summary>

    public float xMinLimit = -20.0f;
    public float xMaxLimit = 80.0f;

    public float yMinLimit = -20.0f;
    public float yMaxLimit = 80.0f;

    /// <summary>
    /// ---------------限制距离-----------------------
    /// </summary>

    public float initDis = 2.0f;
    public float minDis = 2.0f;
    public float maxDis = 10.0f;

    /// <summary>
    /// ------------------鼠标滚轮速度------------------
    /// </summary>

    public float wheelSpeed = 5.0f;

    public float yMoveSpeed = 1.0f;
    public float xMoveSpeed = 1.0f;

    public  float x = 130.0f;

    public  float y = 30.0f;

    private float distance = 0.0f;

    public bool IsOnHove = false ;




    // Use this for initialization
    void Start () 
    {
        InitOrbitCamera(initPosObject);
    }

   // public Transform getOb;
    bool zoomFlag = false;
    // Update is called once per frame
    void Update () 
    {

        //鼠标浮在NGUI滑条上
        if (!IsOnHove)
        {
            //距离相比较
            distance = Vector3.Distance(Vector3.zero,transform.localPosition);

            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; // 鼠标横向输入
            x = ClampAngle(x, xMinLimit, xMaxLimit);
            y += Input.GetAxis("Mouse Y") * ySpeed * -0.02f; //鼠标纵向输入
            y = ClampAngle(y, yMinLimit, yMaxLimit);//得到一个适中 的角度

            if (Input.GetMouseButton(0))
            {
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; // 鼠标横向输入
                x = ClampAngle(x, xMinLimit, xMaxLimit);
                y += Input.GetAxis("Mouse Y") * ySpeed * -0.02f; //鼠标纵向输入
                y = ClampAngle(y, yMinLimit, yMaxLimit);//得到一个适中 的角度
            }
            if (Input.GetMouseButtonDown(1))
            {
                if (!zoomFlag)
                {
                    this.GetComponent<Camera>().fieldOfView = 12;
                }
                else {
                    this.GetComponent<Camera>().fieldOfView = 50;
                }
                zoomFlag = !zoomFlag;

            }

            distance-= Input.GetAxis("Mouse ScrollWheel")*wheelSpeed;//距离是用滚轮控制
        }
        //得到适中的距离
        distance = Mathf.Clamp(distance,minDis,maxDis);
        //得到旋转的角度
        if (moveflag)
        {
            Quaternion localRotation = Quaternion.Euler(y, 0, 0);

            Vector3 localPosition = localRotation * new Vector3(0.0f, 0.0f, -1*distance) + Vector3.zero;

            transform.localRotation = localRotation;
            transform.localPosition = localPosition;

            Quaternion rotationP = Quaternion.Euler(0,x, 0);

            transform.parent.rotation = rotationP;


            if(Mathf.Abs(Input.GetAxis("Horizontal"))>0||Mathf.Abs(Input.GetAxis("Vertical"))>0)
            {
                transform.parent.Translate(new Vector3(Input.GetAxis("Horizontal")*xMoveSpeed, 0, Input.GetAxis("Vertical")*xMoveSpeed)*Time.deltaTime);
            }
            if(Input.GetMouseButton(2))
            {
                transform.parent.transform.Translate(0,Input.GetAxis("Mouse Y") * yMoveSpeed,0);
            }
        }

    }
    /// <summary>
    /// 设置相机以某个物体为中心
    /// </summary>
    /// <param name="cObj"></param>
    public void InitOrbitCamera(Transform cObj)
    {
        if(cObj!=null)
        {
            transform.parent.position = cObj.position;
        }
        else
        {
            transform.parent.position = Vector3.zero;
        }

        transform.localRotation = Quaternion.Euler(y,x,0);
        transform.localPosition = Quaternion.Euler(y,x,0) * new Vector3(0.0f,0.0f,-1*initDis);
    }
    //限制角度
    float ClampAngle(float angle,float min,float max)
    {
        if (angle < -360.0f)
        {
            angle += 360.0f;
        }
        if (angle > 360.0f)
        {
            angle -= 360.0f;
        }
        return Mathf.Clamp (angle, min, max);
    }


    private Vector3 targetPos;
    private bool moveflag = true;
    private bool backFlag = false;

    /// <summary>
    /// 返回目标点
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public IEnumerator BackStartPos(Transform obj)
    {
        backFlag = true;
        yield return 1;
        StartCoroutine(SetCameraTarget(obj));
        yield return 1;
    }

    public float TempY;

    public float TempX;
    /// <summary>
    /// 设置相机的目标点
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public IEnumerator SetCameraTarget(Transform obj)
    {
        TempY = y;

        TempX = x;

        bool isMove = true;
        moveflag = false;

        Quaternion localRotation = Quaternion.Euler(y, 0, 0);


        Quaternion rotationP = Quaternion.Euler(0, x, 0);

        Vector3 camEndPos = localRotation * new Vector3(0.0f, 0.0f, -1 * initDis);
        Vector3 camPos;

        if (obj.transform.GetComponent<Collider>())
        {
            camPos = obj.transform.GetComponent<Collider>().bounds.center;
        }
        else
        {
            camPos = obj.transform.position;
        }
        while (isMove)
        {

            transform.localRotation = Quaternion.Lerp(transform.localRotation, localRotation,0.05f);
            transform.localPosition = Vector3.Lerp(transform.localPosition, camEndPos, 0.05f);
            transform.parent.position = Vector3.Lerp(transform.parent.position, camPos, 0.05f);

            transform.parent.rotation = Quaternion.Lerp(transform.parent.rotation, rotationP, 0.05f);

            yield return new WaitForSeconds(0.01f);

            if ((Vector3.Distance(transform.parent.position, camPos) < 0.005f) && (Mathf.Abs((Vector3.Distance(transform.localPosition, Vector3.zero)) - initDis) < 0.01f))
            {
                isMove = false;

                //恢复输入的y的值
                y = TempY;
                //恢复输入的x的值
                x = TempX;

                moveflag = true;
                transform.parent.position = camPos;
                backFlag = false;

                IsOnHove = false;
                break;
            }

        }
    }
}

这里写图片描述

2019-01-15 11:14:26 wl1611102 阅读数 393

Unity3D_控制摄像机跟随物体

情景一:第三人称跟随

1、效果描述:摄像机一直跟随在物体的后上方,并一直看向物体
2、代码实现:
挂载到Camera上

public class LookAtObject : MonoBehaviour {

	private Transform transformOfTargetObject;//被跟随物体
	private Vector3 offset;//差量
	private Vector3 targetPosition;//
	private float speed=5;
	void Start () {
		transformOfTargetObject=GameObject.Find("Cube").GetComponent<Transform>();
		offset=gameObject.transform.position-transformOfTargetObject.position;
	}
	
	void Update () {
		targetPosition=transformOfTargetObject.position+transformOfTargetObject.TransformDirection(offset);/*一定要
									transformOfTargetObject.TransformDirection(offset),不然摄像机不会跟在物体后面*/
		gameObject.transform.position=Vector3.Lerp(gameObject.transform.position,targetPosition,Time.deltaTime*speed);
		gameObject.transform.LookAt(transformOfTargetObject);
	}

3、结果:在这里插入图片描述

情景二:全局观测:

1、效果描述:对于有两个游戏角色,需要同时观测到他们,并能将他们维持在视野当中
2、代码实现:
代码添加到Camera上

public class LookAtThem : MonoBehaviour {

	public GameObject play1;
	public GameObject play2;
	private Camera m_camera;
	private Vector3 offset;
	private float viewScale;
	
	void Start () {
		offset=gameObject.transform.position-(play1.transform.position+play2.transform.position)/2;
		m_camera=this.GetComponent<Camera>();
		viewScale=m_camera.fieldOfView/Vector3.Distance(play1.transform.position,play2.transform.position);
	}
	
	void Update () {
		this.transform.position=(play1.transform.position+play2.transform.position)/2+offset;
		m_camera.fieldOfView=Vector3.Distance(play1.transform.position,play2.transform.position)*viewScale;
	}
}

3、结果
移开时
在这里插入图片描述
移近时
在这里插入图片描述

2015-09-15 10:09:48 yangyisen0713 阅读数 25404

C#控制WASD键盘前后左右及空格键抬升高度脚本代码如下:

using UnityEngine;
using System.Collections;

public class CameraControl : MonoBehaviour {
	// Use this for initialization
	private GameObject gameObject;
	float x1;
	float x2;
	float x3;
	float x4;
	void Start () {
		gameObject = GameObject.Find ("Main Camera");
	}
	
	// Update is called once per frame
	void Update () {
		//空格键抬升高度
		if (Input.GetKey (KeyCode.Space))
		{
			transform.position =  new Vector3(transform.position.x,transform.position.y + 1,transform.position.z);
		}	 

		//w键前进
		if(Input.GetKey(KeyCode.W))
		{
			this.gameObject.transform.Translate(new Vector3(0,0,50*Time.deltaTime));
		}
		//s键后退
		if(Input.GetKey(KeyCode.S))
		{
			this.gameObject.transform.Translate(new Vector3(0,0,-50*Time.deltaTime));
		}
		//a键后退
		if(Input.GetKey(KeyCode.A))
		{
			this.gameObject.transform.Translate(new Vector3(-10,0,0*Time.deltaTime));
		}
		//d键后退
		if(Input.GetKey(KeyCode.D))
		{
			this.gameObject.transform.Translate(new Vector3(10,0,0*Time.deltaTime));
		}
	}
}

JS控制鼠标移动视角代码如下:

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 = -60F;
	public float maximumY = 60F;

	float rotationY = 0F;

	void Update ()
	{
		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);
		}
	}
	
	void Start ()
	{
		// Make the rigid body not change rotation
		if (GetComponent<Rigidbody>())
			GetComponent<Rigidbody>().freezeRotation = true;
	}
}

unity3d摄像机

阅读数 32572

Unity3D

博文 来自: yu__jiaoshou
没有更多推荐了,返回首页