Skip to content

Instantly share code, notes, and snippets.

@todorok1
Last active April 9, 2018 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todorok1/582084a727850cdb63c2b2d3e31707b3 to your computer and use it in GitHub Desktop.
Save todorok1/582084a727850cdb63c2b2d3e31707b3 to your computer and use it in GitHub Desktop.
Unityチュートリアル・キー入力のスクリプト。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SphereBooster : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// Input.GetKeyUpはキーが一度押された後、それが離された時にTrueを返す
// KeyCode.Spaceはスペースキーを表す
if (Input.GetKeyUp(KeyCode.Space)){
// ボールを飛ばす処理をStart()からここに移動させる
// 力を加える方向
Vector3 forceDirection = new Vector3(1.0f, 1.0f, 0f);
// 加える力の大きさ
float forceMagnitude = 10.0f;
// 向きと力の計算
Vector3 force = forceMagnitude * forceDirection;
// SphereオブジェクトのRigidbodyコンポーネントへの参照を取得
Rigidbody rb = gameObject.GetComponent<Rigidbody>();
// 力を加えるメソッド
rb.AddForce(force, ForceMode.Impulse);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment