zoukankan      html  css  js  c++  java
  • unity官网教程——Roll-a-ball tutorial——1移动的小球

    参考网址:https://unity3d.com/cn/learn/tutorials/projects/roll-ball-tutorial/moving-player?playlist=17141

        http://blog.csdn.net/u013681344/article/details/51319248?locationNum=9

    实现结果:键盘按钮控制小球的移动

    代码:

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 
     5 public class PlayerController : MonoBehaviour
     6 {
     7     public float mBallSpeed = 0.0f;
     8 
     9     private Rigidbody mPlayerRgbody = null;
    10     private float mMoveX = 0.0f;
    11     private float mMoveZ = 0.0f;
    12     private Vector3 mMovement = Vector3.zero;
    13 
    14     private void Start()
    15     {
    16         mPlayerRgbody = gameObject.GetComponent<Rigidbody>();
    17         if (mPlayerRgbody == null)
    18         {
    19             Debug.LogError("this rigidBody is null !!!");
    20         }
    21     }
    22 
    23     // 如果启用 MonoBehaviour,则每个固定帧速率的帧都将调用此函数
    24     private void FixedUpdate()
    25     {
    26         mMoveX = Input.GetAxis("Horizontal");
    27 
    28         Debug.LogError("X:" + mMoveX);
    29 
    30         mMoveZ = Input.GetAxis("Vertical");
    31 
    32         mMovement.x = mMoveX;
    33         mMovement.z = mMoveZ;
    34 
    35         mPlayerRgbody.AddForce(mMovement * mBallSpeed, ForceMode.Force);
    36     }
    37 
    38 }

    从日志可以看出 

    Debug.LogError("X:" + mMoveX);  当按下D按键不松时 mMoveX是不断从0增大到1.0f

    总结:

    Input类的GetAxis()函数:返回值为浮点数,范围在-1和1之间(重置坐标设置)
    Input.GetAxis()与Input.GetKey()和Input.Button()原理类似,但有根本区别,Input.GetKey()和Input.Button()返回布尔值,表示按钮是否被按下,而GetAxis返回浮点数,范围在1和-1之间,如果想在Input Manager中重置坐标设置,按键只需要设置"Postive Button",而坐标轴需要设置"Positive&NegativeButton",

    "Gravity","Seneitivity(灵敏度)","Dead","Snap"
    Gravity:表示按钮松开后,返回值归零的速度,Gravity越大,归零速度越快
    Sensitivity:表示按下按钮后,返回值到达1或者-1的速度,值越大,速度越快,反之则越平滑
    如果我们使用摇杆来控制坐标轴,我们不需要让他的返回值过小,可以设置Dead,Dead值越大,摇杆也需要移动更大距离才能获取返回值
    勾选Snap选项当正反按钮同时按下时会返回0
    如果想要获取水平和垂直方向的输入,只需加入代码Input.GetAxis("Horizontal")还有Input.GetAxis("Vertical")
    你可以使用Input.GetAxisRaw("Horizonta l")只返回整数值,不需要设置"Gravity","Seneitivity"

    改变自己
  • 相关阅读:
    spark java wordCount实例
    SpringMVC 商城项目
    jstl 遍历数据
    jquery 方法总结
    8ch
    7ch
    使用vuex实现组件间传值
    Vue父子组件之间传值
    从浏览器输入URL地址到最终页面渲染完成,发生了什么?
    数组去重
  • 原文地址:https://www.cnblogs.com/sun-shadow/p/7881789.html
Copyright © 2011-2022 走看看