Skip to content

Instantly share code, notes, and snippets.

@LightGive
Created October 27, 2017 07:35
Show Gist options
  • Save LightGive/2c4a55a949e04676bdf36829dc865a02 to your computer and use it in GitHub Desktop.
Save LightGive/2c4a55a949e04676bdf36829dc865a02 to your computer and use it in GitHub Desktop.
斜方投射で頂点座標に達した時の座標の予測
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test2 : MonoBehaviour {
[SerializeField]
private Vector3 vec;
[SerializeField]
private GameObject dummySphere;
private Rigidbody rigid;
private bool isShot = false;
private float timeCnt = 0.0f;
private float stopTime = 0.0f;
void Start()
{
rigid = GetComponent<Rigidbody>();
if (!rigid)
{
rigid = gameObject.AddComponent<Rigidbody>();
}
rigid.isKinematic = true;
dummySphere.transform.position = transform.position;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
var t = vec.y / (-Physics.gravity.y);
stopTime = t;
var y = transform.position.y + (vec.y * t) - 0.5f * (-Physics.gravity.y) * Mathf.Pow(t, 2.0f);
var x = transform.position.x + (vec.x * t);
var z = transform.position.z + (vec.z * t);
var pos = new Vector3(x, y, z);
dummySphere.transform.position = pos;
Debug.Log(t.ToString("F2") + "秒後の座標は" + pos.ToString("F5"));
rigid.isKinematic = false;
rigid.AddForce(vec, ForceMode.VelocityChange);
isShot = true;
timeCnt = 0.0f;
}
if (!isShot)
return;
timeCnt += Time.deltaTime;
if (timeCnt >= stopTime)
{
isShot = false;
Debug.Log("予測との誤差:" + Vector3.Distance(transform.position, dummySphere.transform.position).ToString("F5"));
Debug.Break();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment