Skip to content

Instantly share code, notes, and snippets.

@todorok1
Last active April 10, 2018 09:34
Show Gist options
  • Save todorok1/b2c7cac2e3be992317f08ed738f5f34f to your computer and use it in GitHub Desktop.
Save todorok1/b2c7cac2e3be992317f08ed738f5f34f to your computer and use it in GitHub Desktop.
Unityチュートリアル・キー入力のスクリプト。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SphereBooster : MonoBehaviour {
// 飛行中フラグ
bool isFlying = false;
// ボタン押下フラグ
bool isBoostPressed = false;
// Sphereオブジェクトの初期位置格納用ベクトル
Vector3 initPosition = Vector3.zero;
void Start(){
initPosition = gameObject.transform.position;
}
void Update(){
// Input.GetKeyUpはキーが一度押された後、それが離された時にTrueを返す
if (Input.GetKeyUp(KeyCode.Space)){
isBoostPressed = true;
}
}
void FixedUpdate(){
if (isBoostPressed){
if (isFlying){
// 飛行中の処理
// 運動の停止
Rigidbody rb = gameObject.GetComponent<Rigidbody>();
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
// 初期位置に移動させる
gameObject.transform.position = initPosition;
} else {
// ボールを飛ばす処理
// 力を加える方向
Vector3 forceDirection = new Vector3(1.0f, 1.0f, 0f);
// 加える力の大きさ
float forceMagnitude = 10.0f;
// 向きと力の計算
Vector3 force = forceMagnitude * forceDirection;
// 力を加えるメソッド
Rigidbody rb = gameObject.GetComponent<Rigidbody>();
rb.AddForce(force, ForceMode.Impulse);
}
// 飛行中フラグの切り替え
isFlying = !isFlying;
// どちらの処理をしてもボタン押下フラグをfalseに
isBoostPressed = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment