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

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.

主站蜘蛛池模板: 汉沽区| 墨竹工卡县| 屏南县| 白山市| 晋宁县| 屯留县| 新宁县| 泰顺县| 东源县| 民勤县| 吉木乃县| 博兴县| 竹溪县| 织金县| 仁寿县| 临海市| 新乡市| 小金县| 和田县| 甘孜| 西贡区| 贵阳市| 太康县| 盐城市| 宝应县| 甘肃省| 龙州县| 分宜县| 耿马| 长白| 陕西省| 淮安市| 清镇市| 桦甸市| 镇原县| 沂南县| 龙陵县| 儋州市| 祥云县| 济南市| 嘉黎县|