Skip to content

Instantly share code, notes, and snippets.

@ppengotsu
Created December 17, 2021 06:20
Show Gist options
  • Save ppengotsu/bc973de80b48d380098de9c95f756290 to your computer and use it in GitHub Desktop.
Save ppengotsu/bc973de80b48d380098de9c95f756290 to your computer and use it in GitHub Desktop.
ARDKのブログ2のボール飛ばすコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ActionManager : MonoBehaviour
{
[SerializeField]
private GameObject ballPrefab;
[SerializeField]
private Camera mainCamera;
void Start()
{
}
void Update()
{
#if UNITY_EDITOR
//
// UnityEditorで動かしてるとき
//
if(Input.GetMouseButtonDown(0)){
//
// 画面がクリックされた時の処理
//
FireBall();
}
#else
//
// ビルドして実機で動かしてるとき
//
if(Input.touchCount>0){
Touch touch0 = Input.GetTouch(0);
if(touch0.phase == TouchPhase.Began){
//
// 画面がタッチされた時の処理
//
FireBall();
}
}
#endif
}
/// <summary>
/// ボールオブジェクトを生成して飛ばします。
/// </summary>
private void FireBall(){
float power = 2.0f;//ボールを飛ばす強さ
GameObject ballObject = GameObject.Instantiate(ballPrefab, mainCamera.transform.position, Quaternion.identity);
ballObject.GetComponent<Rigidbody>().AddForce(mainCamera.transform.forward* power);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment