【问题标题】:Vector3.Distance for comparing mouse and GameObject distanceVector3.Distance 用于比较鼠标和游戏对象的距离
【发布时间】:2020-02-11 16:36:04
【问题描述】:

我有一些 mouseinput ,我将其转换为 Vector3。 现在我使用这个输入并计算了鼠标输入和我的游戏对象位置之间的距离。 该方法返回一个浮点数作为我的距离,我将它与我的 MaxDistance 浮点数进行了比较。 因此,当我的 Inputdistance 小于或等于我的 MaxDistance 时,它​​应该破坏我的 GameObject。 但是当我运行我的游戏时,没有任何效果。 我也尝试增加 MaxDistance 值,但也没有用。

这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tropfen : MonoBehaviour
{
public float speed = 10.0f;
Rigidbody2D rb;
AudioSource source;
[SerializeField] AudioClip[] soundClips;
[SerializeField] GameObject particleEffect;
[SerializeField] float maxDistance = 20f;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
    rb.velocity = new Vector2(0, -speed * Time.deltaTime);
    source = GetComponent<AudioSource>();
    source.clip = soundClips[Random.Range(0, soundClips.Length)];
}

// Update is called once per frame
void Update()
{
    Vector3 screenPos = Input.mousePosition;
    Camera.main.ScreenToWorldPoint(screenPos);

    float TropfenTouchDistance = Vector3.Distance(screenPos, gameObject.transform.position);

    if ( TropfenTouchDistance <= maxDistance)
    {
        TropfenDestruction();
    }
}

private void TropfenDestruction()
{
    Destroy(gameObject);
    AudioSource.PlayClipAtPoint(source.clip, transform.position);
    GameObject effect = Instantiate(particleEffect, transform.position, Quaternion.identity) as 
    GameObject;
    Destroy(effect, 2f);
    FindObjectOfType<Gameplay>().IncreaseScore(1);
}
}

【问题讨论】:

  • 您没有使用 Camera.main.ScreenToWorldPoint 的返回值 - 这可能是计算距离时的问题吗?

标签: c# unity3d vector game-engine


【解决方案1】:

您的screenPos 仍在屏幕像素空间中,因为您没有使用ScreenToWorldPoint 的输出

var screenPos = Input.mousePosition;
var worldPos = Camera.main.ScreenToWorldPoint(screenPos);

// It's redundant going through the gameObject here btw
var TropfenTouchDistance = Vector3.Distance(worldPos, transform.position);

if ( TropfenTouchDistance <= maxDistance)
{
    TropfenDestruction();
}

无论如何请注意ScreenToWorldPoint 还需要一个Z 组件

屏幕空间位置(通常是鼠标 x、y),加上 z 位置作为深度(例如,相机剪裁平面)。

指示应将点投影到世界中与给定Camera 的距离。


为了克服这个问题,您可以使用数学上的Plane,这样您就可以在Plane.Raycast 中使用ScreenPointToRay 准确地获得投影到对象旁边的点

// In general store the camera reference as Camera.main is very expensive!
[SerializeField] private Camera _camera;


private void Update()
{
    if(!_camera) _camera = Camera.main;

    var plane = new Plane(_camera.transform.forward, transform.position);
    var ray = _camera.ScreenPointToRay(Input.mousePosition);
    if(plane.Raycast(ray, out var hitDistance))
    {  
        var worldPoint = ray.GetPoint(hitDistance);
        var TropfenTouchDistance = Vector3.Distance(worldPos, transform.position);

        if ( TropfenTouchDistance <= maxDistance)
        {
            TropfenDestruction();
        }
    }
}

【讨论】:

  • 所以我使用了你建议的代码,但是当我的游戏对象被实例化时,它们会立即被销毁。
  • 好吧,因为您拥有它,您的 Update 方法始终会被执行,而不是等待任何用户输入......您的 maxDistance20 单位可能很大,因此所有条件总是满足销毁一个对象?
猜你喜欢
  • 1970-01-01
  • 2015-12-25
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多