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

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.

主站蜘蛛池模板: 磐安县| 汨罗市| 蒙城县| 嘉兴市| 广平县| 汝城县| 绥化市| 遵义县| 汉沽区| 潮州市| 奎屯市| 乐清市| 陵水| 渭南市| 合川市| 陆丰市| 永胜县| 邹城市| 双柏县| 平阳县| 富民县| 前郭尔| 桓仁| 灵丘县| 大埔区| 礼泉县| 汤阴县| 华蓥市| 白城市| 松原市| 卢湾区| 家居| 秭归县| 元朗区| 昆山市| 六安市| 金溪县| 土默特右旗| 博客| 柳河县| 乡城县|