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

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.

主站蜘蛛池模板: 安岳县| 湘潭县| 徐闻县| 谢通门县| 来凤县| 蛟河市| 乐平市| 古浪县| 邵阳市| 信丰县| 上蔡县| 庆城县| 东乡| 清苑县| 仙桃市| 兴隆县| 新安县| 柯坪县| 莱芜市| 南雄市| 东方市| 永清县| 浦城县| 贵港市| 宣恩县| 宜州市| 星座| 黑山县| 商城县| 高要市| 东明县| 万盛区| 白银市| 兴安盟| 七台河市| 纳雍县| 酒泉市| 乌鲁木齐市| 永嘉县| 龙井市| 扬州市|