码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3d 札记-Tanks Tutorial 知识点汇总

时间:2016-08-22 19:52:47      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

1\如何通过输入来控制物体的移动

               前提:物体必须添加RigidBody(刚体)组件

     1.初始化 组件

     选择在Awake()或者Start()方法中RigidBody rb = GetComponent<RigidBody>();

     2.获得输入

     在 void Update()方法中写入 获取代码

   

private float m_InputVerticalValue;
private float m_InputHorizontalValue;
void Update(){
     m_InputVerticalValue = Input.GetAxis("Vertical");
     //Input.GetAxis(String AxisName),默认正向为1,负向为 -1 ,不动为 0 
     m_InputHorizontalValue = Input.GetAxis("Horizontal");
}

      3.在Update() 或者 FixedUpdate()中利用 得到的输入值  m_InputVerticalValue,m_InputHorizontalValue;

//第一种RigidBody.AddForce()
private float force  = 10;
void Update(){
   ……
  rb.AddForce(force*m_InputVerticalValue*Time.deltaTime);

}


//另外一种 RigidBody.MovePosition() RigidBody.MoveRotation();
private void FixedUpdate()
    {
        // Move and turn the tank.
		Move();
		Turn();
    }


    private void Move()
    {
        // Adjust the position of the tank based on the player‘s input.
		Vector3 movement = transform.forward*m_Speed*m_MovementInputValue*Time.deltaTime;
		m_Rigidbody.MovePosition (transform.position + movement);
    }


    private void Turn()
    {
		float turn = m_TurnSpeed * m_TurnInputValue * Time.deltaTime;
        // Adjust the rotation of the tank based on the player‘s input.
		Quaternion turnQuaternion  =  Quaternion.Euler(0f,turn,0f);
		m_Rigidbody.MoveRotation (transform.rotation * turnQuaternion);
    }

 

 

2\Camera如何随着坦克移动,调整位置及视野?              Position            Zoom

           解决方案 :   通过各个坦克的三维位置 计算得出平均位置 

                             通过找到各个坦克 离平均位置的 距离         得到视野的大小

 技术分享

 

using UnityEngine;

public class CameraControl : MonoBehaviour
{
    public float m_DampTime = 0.2f;                 
    public float m_ScreenEdgeBuffer = 4f;           
    public float m_MinSize = 6.5f;                  
    public Transform[] m_Targets; 


    private Camera m_Camera;                        
    private float m_ZoomSpeed;                      
    private Vector3 m_MoveVelocity;                 
    private Vector3 m_DesiredPosition;              


    private void Awake()
    {
        m_Camera = GetComponentInChildren<Camera>();
    }


    private void FixedUpdate()
    {
        Move();
        Zoom();
    }


    private void Move()
    {
        FindAveragePosition();

        transform.position = Vector3.SmoothDamp(transform.position, m_DesiredPosition, ref m_MoveVelocity, m_DampTime);
    }


    private void FindAveragePosition()
    {
        Vector3 averagePos = new Vector3();
        int numTargets = 0;

        for (int i = 0; i < m_Targets.Length; i++)
        {
            if (!m_Targets[i].gameObject.activeSelf)
                continue;

            averagePos += m_Targets[i].position;
            numTargets++;
        }

        if (numTargets > 0)
            averagePos /= numTargets;

        averagePos.y = transform.position.y;

        m_DesiredPosition = averagePos;
    }


    private void Zoom()
    {
        float requiredSize = FindRequiredSize();
        m_Camera.orthographicSize = Mathf.SmoothDamp(m_Camera.orthographicSize, requiredSize, ref m_ZoomSpeed, m_DampTime);
//SmoothDamp是一个渐变的过程
//需要ref ZoomSpeed 和 m_DampTime来计算渐变过程
    }


    private float FindRequiredSize()
    {
        Vector3 desiredLocalPos = transform.InverseTransformPoint(m_DesiredPosition);

        float size = 0f;

        for (int i = 0; i < m_Targets.Length; i++)
        {
            if (!m_Targets[i].gameObject.activeSelf)
                continue;

            Vector3 targetLocalPos = transform.InverseTransformPoint(m_Targets[i].position);
//以当前视野为参考系,3维转2维
            Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos;
//计算相对位置
            size = Mathf.Max (size, Mathf.Abs (desiredPosToTarget.y));
//取最大Y方向距离
            size = Mathf.Max (size, Mathf.Abs (desiredPosToTarget.x) / m_Camera.aspect);
//取最大的符合分辨率的距离
        }
        
        size += m_ScreenEdgeBuffer;

        size = Mathf.Max(size, m_MinSize);

        return size;
    }


    public void SetStartPositionAndSize() //直接调整位置和视野,不用渐变过程
    {
        FindAveragePosition();

        transform.position = m_DesiredPosition;

        m_Camera.orthographicSize = FindRequiredSize();
    }
}

Unity3d 札记-Tanks Tutorial 知识点汇总

标签:

原文地址:http://www.cnblogs.com/dongfangliu/p/5796533.html

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