LoginSignup
3
2

More than 5 years have passed since last update.

[ 斜方投射 ] 落下地点を予測する

Posted at

こちらの記事で、任意の場所にオブジェクトを飛ばすことができますが、同様に、角度と大きさを決めて落下地点を予測することができます

DrawParabolicLine.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrawParabolicLine : MonoBehaviour {
    public Vector3 dir = new Vector3(10f, 10f, 10f);
    public Vector3 origin = Vector3.zero;
    public float calcTime = 5f;
    [SerializeField, Range(0.001f,0.5f)]public float timeScale = 0.1f;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        Vector3 pos = origin;
        Vector3 newPos = pos;
        float time = 0f;
        List<Vector3> posList = new List<Vector3>();
        while(time<calcTime){
            Vector3 dirXZ = new Vector3(dir.x, 0f, dir.z);
            // h=v0t+1/2*g*t^2
            float h = dir.y*time + 0.5f * Physics.gravity.y * time * time;
            newPos = origin + dirXZ * time + Vector3.up * h;
            posList.Add(newPos);
            Debug.DrawLine(pos, newPos, Color.yellow);
            pos = newPos;
            time += timeScale;
        }
    }
}

スクリーンショット 2018-02-27 9.00.21.png

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2