- 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.
推薦閱讀
- HTML5移動Web開發技術
- Windows系統管理與服務配置
- Python Game Programming By Example
- 面向STEM的Scratch創新課程
- Spring Cloud、Nginx高并發核心編程
- Java EE 7 Development with NetBeans 8
- 算法訓練營:提高篇(全彩版)
- Mastering Android Development with Kotlin
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- C語言程序設計上機指導與習題解答(第2版)
- 0 bug:C/C++商用工程之道
- Python程序設計開發寶典
- 大學計算機應用基礎(Windows 7+Office 2010)(IC3)
- Java從入門到精通(視頻實戰版)
- 分布式數據庫HBase案例教程