官术网_书友最值得收藏!

Putting it all together

With all of the stuff we've been talking about, we can now have the final version of the script, which looks like the following:

using UnityEngine; 

/// <summary>
/// Responsible for moving the player automatically and
/// reciving input.
/// </summary>
[RequireComponent(typeof(Rigidbody))]

public class PlayerBehaviour : MonoBehaviour
{
/// <summary>
/// A reference to the Rigidbody component
/// </summary>
private Rigidbody rb;

[Tooltip("How fast the ball moves left/right")]
public float dodgeSpeed = 5;


[Tooltip("How fast the ball moves forwards automatically")]
[Range(0, 10)]
public float rollSpeed = 5;

/// <summary>
/// Use this for initialization
/// </summary>
void Start ()
{
// Get access to our Rigidbody component
rb = GetComponent<Rigidbody>();
}

/// <summary>
/// Update is called once per frame
/// </summary>
void Update ()
{
// Check if we're moving to the side
var horizontalSpeed = Input.GetAxis("Horizontal") *
dodgeSpeed;

// Apply our auto-moving and movement forces
rb.AddForce(horizontalSpeed, 0, rollSpeed);
}
}

I hope that you also agree that this makes the code easier to understand and better to work with.

主站蜘蛛池模板: 永嘉县| 新野县| 周口市| 元氏县| 株洲县| 七台河市| 屏边| 南乐县| 凤山市| 固镇县| 施秉县| 临猗县| 米易县| 花莲县| 扎兰屯市| 怀远县| 平顶山市| 龙泉市| 宝鸡市| 普兰店市| 菏泽市| 西和县| 科尔| 名山县| 大同市| 乌拉特后旗| 浦江县| 新竹县| 浪卡子县| 湘潭县| 浦东新区| 鞍山市| 鄂尔多斯市| 胶南市| 汤原县| 宁津县| 德化县| 衡水市| 龙陵县| 诸城市| 垣曲县|