- 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.
推薦閱讀
- OpenStack Cloud Computing Cookbook(Third Edition)
- 樂高機(jī)器人設(shè)計(jì)技巧:EV3結(jié)構(gòu)設(shè)計(jì)與編程指導(dǎo)
- Lua程序設(shè)計(jì)(第4版)
- MySQL數(shù)據(jù)庫管理與開發(fā)(慕課版)
- The HTML and CSS Workshop
- Angular開發(fā)入門與實(shí)戰(zhàn)
- Orleans:構(gòu)建高性能分布式Actor服務(wù)
- 零代碼實(shí)戰(zhàn):企業(yè)級應(yīng)用搭建與案例詳解
- 工業(yè)機(jī)器人離線編程
- INSTANT Apache ServiceMix How-to
- R語言數(shù)據(jù)挖掘:實(shí)用項(xiàng)目解析
- Shopify Application Development
- jMonkeyEngine 3.0 Beginner’s Guide
- LibGDX Game Development By Example
- matplotlib Plotting Cookbook