- Unity 2017 Mobile Game Development
- John P. Doran
- 167字
- 2021-07-02 22:27:45
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.
推薦閱讀
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- 軟件項目估算
- JS全書:JavaScript Web前端開發指南
- NGINX Cookbook
- Visual Studio Code 權威指南
- 分布式數據庫原理、架構與實踐
- Python預測分析實戰
- 深入理解Kafka:核心設計與實踐原理
- Instant AutoIt Scripting
- 測試基地實訓指導
- VB語言程序設計教程(第2版)
- Learning Network Programming with Java
- Modular Programming with PHP 7
- KnockoutJS Essentials
- Drupal 8 Quick Start Guide