Input.GetAxis用法

GetAxis()两种:

Vertical:获得垂直方向。

Horizontal:获得水平方向。

 

控制坦克移动旋转脚本如下:

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TankController : MonoBehaviour
{
    private Rigidbody m_Rigidbody;

    private float m_MoveAxis = 0.0f;
    private float m_TurnAxis = 0.0f;

    private float moveSpeed = 12.0f; /*移动速度*/
    private float turnSpeed = 180.0f; /*旋转速度*/

    void Start()
    {
        m_Rigidbody = gameObject.GetComponent();
    }

    void Update()
    {
        m_MoveAxis = Input.GetAxis("Vertical"); /*检测垂直方向键*/
        m_TurnAxis = Input.GetAxis("Horizontal"); /*检测水平方向键*/
    }

    void FixedUpdate()
    {
        Move();
        Turn();
    }

    /// 
    /// 向前或向后移动
    /// 
    void Move()
    {
        Vector3 moveChange = transform.forward* m_MoveAxis * Time.deltaTime * moveSpeed;  /*移动增量*/
        m_Rigidbody.MovePosition(transform.position + moveChange);
    }

    /// 
    /// 旋转车身
    /// 
    void Turn()
    {
        float Change = m_TurnAxis * Time.deltaTime * turnSpeed;
        Quaternion turnChange = Quaternion.Euler(0.0f, Change, 0.0f);
        m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnChange);
    }
}

 

你可能感兴趣的:(unity-3d)