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

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.

主站蜘蛛池模板: 波密县| 遂昌县| 定州市| 金阳县| 凤凰县| 宁都县| 邯郸县| 册亨县| 泽州县| 鹿泉市| 弥渡县| 新宁县| 民和| 澄江县| 武汉市| 蒙自县| 闸北区| 长顺县| 五大连池市| 岗巴县| 信阳市| 边坝县| 哈巴河县| 翁牛特旗| 太仆寺旗| 莒南县| 贵南县| 镇赉县| 松滋市| 广水市| 盐津县| 汉源县| 石阡县| 泸定县| 宣城市| 冷水江市| 正阳县| 许昌市| 吉首市| 江城| 古交市|