Unity c # Flick объект в направлении независимо от угла камеры - PullRequest
0 голосов
/ 08 декабря 2018

У меня есть следующий код, чтобы переместить объект вперед, проводя в нужном мне направлении.Проблема заключается в том, что угол поворота моей камеры поворачивается, поэтому всякий раз, когда я перемещаю объект в центр или влево, щелчок не является точным.Когда я щелкаю вправо, объект брошен точно.Как я могу настроить этот код так, чтобы объект был повернут в направлении мирового пространства независимо от вращения камеры, пожалуйста.Спасибо.

Код: `открытый класс SwipeScript: MonoBehaviour {

public float force = 2f;
public bool isTarget = false;
public float zFactor = 2f;

public Vector3 startPosition;

private Vector2 startSwipe;
private Vector2 endSwipe;
private Rigidbody currentBoxRb;

void Start()
{
    currentBoxRb = StackerManager.currentBox.GetComponent<Rigidbody>();
    startPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        startSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    }

    if (Input.GetMouseButtonUp(0))
    {

        endSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
        if (isTarget == true)
        {
            Swipe();
            isTarget = false;

        }


    }
}
void Swipe()
{
    Vector3 swipe = endSwipe - startSwipe;
    swipe.z = swipe.y / zFactor;
    swipe.y = -0.001f;
    currentBoxRb.isKinematic = false;
    currentBoxRb.AddForce(swipe * force, ForceMode.Impulse);

}

private void OnMouseDown()
{

    currentBoxRb.constraints = RigidbodyConstraints.None;
    isTarget = true;

}

}

...