繁体   English   中英

根据 Raycast 命中 position 重新缩放图像?

[英]Rescale an Image depending on the Raycast hit position?

我在线条渲染器的末尾使用线条渲染器和圆形 png。 当光线投射碰到碰撞的 object 时,圆形 png 出现在行尾。 我面临的问题是; 如果 object 距离 10 米,我可以轻松看到圆形 png,但如果对撞机小于 4 米,则图像太大。 我如何重新缩放图像,以便如果它更远,它保持不变,如果它接近,那么它会缩小,以便它看起来相似,如果它远或近。

Default Scale of the GreenPoint_Raycast: X, Y, Z: 0.001323662

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

public class raycastLineRenderer : MonoBehaviour {
    public GameObject lineRenderer;
    RaycastHit hit;

    public GameObject RightHandAnchor;

    public GameObject GreenPoint_Raycast;

    // Start is called before the first frame update
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        int layerMask = LayerMask.GetMask ("RaycastTarget");

        if (Physics.Raycast (RightHandAnchor.transform.position, RightHandAnchor.transform.TransformDirection (Vector3.forward), out hit, Mathf.Infinity, layerMask)) {

            GreenPoint_Raycast.GetComponent<Image>().enabled = true; 
            GreenPoint_Raycast.transform.position = (hit.point); //position is right, scaling is the problem

            if (hit.collider) {

                    Debug.Log ("Hit: " + hit.collider.name);

                    if (hit.collider.name == "Mesh_1") {
                    } else
                    if (hit.collider.name == "Mesh_2") {
                    } else
                    if (hit.collider.name == "Mesh_3") {
                    } 

            } else {

                lineRenderer.GetComponent<LineRenderer> ().SetPosition (1, new Vector3 (0, 0, 5000));
                GreenPoint_Raycast.GetComponent<Image>().enabled = false;

            }
        } else {
            GreenPoint_Raycast.GetComponent<Image>().enabled = false;
        }
    }
}

更新:

命中点的距离可以用光线投射命中点计算为:

Debug.Log("hit at distance from RightHandAnchor to object: "+hit.distance);

有了这个,我想用 Lerp 重新调整 GreenPoint_Raycast 的大小?

我认为你需要使用一个简单的比例。

dist1/scale1 = dist2/scale2

如果你说 10 米的比例是正确的(假设你用dist变量测量它),我们可以按比例排列这些数字。

10 / 0.001323662 = dist / scale

所以所需的比例= 0.001323662 * dist / 10

更新:好吧,你删除了dist的计算

但我猜它是Vector3.Distance(hit.point, RightHandAnchor.transform.position)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM