0
ボールが投げられた場所に作業をしています。右にスワイプすれば、その方向に進みますが、それだけです。 もし私がスワイプしたり、画面上に残っていれば、ボールはまだ右にスローされます。だから私の質問は、スワイプする方向にスワイプするためにコード内で何を変更できるのですか?Unity3Dボールはスワイプで右に撃つ
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
}
private float length = 0;
private bool SW = false;
private Vector3 final;
private Vector3 startpos;
private Vector3 endpos;
public GameObject basketball; //Basketball Obj
public Rigidbody rbody;// Basketball Obj
public float Force = 0.2f;
void Update()
{
rbody = basketball.GetComponent<Rigidbody>();
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
final = Vector3.zero;
length = 0;
SW = false;
Vector2 touchDeltaPosition = Input.GetTouch(0).position;
startpos = new Vector3(touchDeltaPosition.x, 0, touchDeltaPosition.y);
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
SW = true;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Canceled)
{
SW = false;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
SW = false;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
if (SW)
{
Vector2 touchPosition = Input.GetTouch(0).position;
endpos = new Vector3(touchPosition.x, 0, touchPosition.y);
final = endpos - startpos;
length = final.magnitude;
rbody.AddForce(new Vector3(touchPosition.x, touchPosition.x + touchPosition.y, touchPosition.y) * Force);
}
}
}
void OnGUI()
{
GUI.Box(new Rect(50, 300, 500, 30), "length: " + length);
}
}
力は一切適用されていません。 – Lynnstrum
'startPos'と' endPos'の値を一度ログに記録して、正しい値を受け取っているかどうかを確認してください。また、 'AddForce()'関数の下に 'SW = false'を追加すると、変数名' final'が 'fin'のように変更されます。 –
GUIを使用して開始位置と終了位置を追跡しました。画面全体をスワイプすると両方とも更新されています。 – Lynnstrum