教程Roll-a-ball tutorialMoving the Player 解读

来源:互联网 发布:服务器安全防护软件 编辑:程序博客网 时间:2024/05/17 07:59
using UnityEngine;using System.Collections;public class PlayerController : MonoBehaviour {    public float speed;    private Rigidbody rb;    void Start ()    {        rb = GetComponent<Rigidbody>();    }    void FixedUpdate ()    {        float moveHorizontal = Input.GetAxis ("Horizontal");        float moveVertical = Input.GetAxis ("Vertical");        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);        rb.AddForce (movement * speed);    }}

分析:

void Start ()    {        rb = GetComponent<Rigidbody>();    }

官方的例子从开始就隐藏着规范,为什么要这么写,一般的程序员会写成下面这样

    void Start ()    {          }

    void FixedUpdate ()    {        float moveHorizontal = Input.GetAxis ("Horizontal");        float moveVertical = Input.GetAxis ("Vertical");        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);        GetComponent<Rigidbody>().AddForce (movement * speed);    }
不对,为什么不对:    点击打开链接


0 0
原创粉丝点击