1.3Dでプロジェクトを作成したら3dギズモを以下のように調整する
![](https://joytas.net/wp-content/uploads/2020/06/img1.png)
2.新規Cubeを作成しGroundにリネーム。transformを以下のように調整する
![](https://joytas.net/wp-content/uploads/2020/06/img71-1024x194.png)
3.カメラの調整。
メインカメラを選択して以下のようにtransformを調整する
![](https://joytas.net/wp-content/uploads/2020/06/img72-1024x223.png)
だいたい以下のような構図になればOKだ
![](https://joytas.net/wp-content/uploads/2020/06/img73.png)
4.Ballの作成
新規Sphereを作成しBallにリネーム。transformを調整する
![](https://joytas.net/wp-content/uploads/2020/06/img74-1024x187.png)
新規にマテリアルを作成し、赤色に設定し、ballにアタッチ
![](https://joytas.net/wp-content/uploads/2020/06/img75-1024x931.png)
Rigidbodyを付与する。値は特に変更しなくてよい。
新規にPhysicMaterialを作成しBallとリネーム。下のように設定しBallにアタッチする
![](https://joytas.net/wp-content/uploads/2020/06/img78-1024x890.png)
実行してみよう。
ボールが落下し少しはねたら成功だ。
5.ボールを飛ばす処理の作成
今回はマウスでドラッグすることにより打ち上げる角度やパワーを調整する仕組みにする。
新規C#スクリプトよりBallControllerを作成し以下のように記述する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sphere : MonoBehaviour
{
Vector2 startPos;
Vector2 endPos;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetMouseButtonDown(0)) {
startPos = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0)) {
endPos = Input.mousePosition;
Vector2 dir = startPos - endPos;
Vector3 vec = new Vector3(0, dir.y, dir.x);
rb.AddForce(vec*3f );
}
}
}
スクリプトをBallにアタッチして実行してみよう。画面を右上から左下に向かってドラッグして離す。
![](https://joytas.net/wp-content/uploads/2020/06/img77-965x1024.png)
ボールが飛んでいけば成功だ。ドラッグする際の角度がボールの打ち上げ角度になるのでいろいろ試してもらいたい。
コメント