在 unity3d 中没有向正确的方向投掷球?



我正在制作一个游戏,玩家在 3D 视图中投掷篮球,但它可能无法正确投掷。当玩家向上移动鼠标时,我需要在 z 方向上正确投掷球。

我想要的游戏参考链接:

参考链接 1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; 
using UnityEngine.SceneManagement;
public class BallController : MonoBehaviour
{
public static BallController instance;
[SerializeField]
private GameObject Player;
private Vector3 touchStart;
private Vector3 touchEnd;
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseUp ()
{
ThrowBall ();
}
public void ThrowBall ()
{
touched = true;
Vector3 pos1 = new Vector3 ();
Vector3 pos2 = new Vector3 ();
touchStart.z = Camera.main.nearClipPlane - (.2f);
touchEnd.z = Camera.main.nearClipPlane - (0.08f);
pos1 = Camera.main.ScreenToWorldPoint (touchStart);
pos2 = Camera.main.ScreenToWorldPoint (touchEnd);
Vector3 v = pos2 - pos1; 
v.z = 0.02f;
Player.GetComponent<Rigidbody> ().AddForce (v * 600, ForceMode.Impulse);
}
}

你得到局部的 z 轴,使用 transform.forward:

Vector3 v = pos2 - pos1; 
v.z = 0.02f * transform.forward;
Player.GetComponent<Rigidbody> ().AddForce (v * 600, ForceMode.Impulse);

或者你可以像

v.z = 0.02f;
Player.GetComponent<Rigidbody> ().AddForce (v * 600 * transform.forward, ForceMode.Impulse);

最新更新