简体   繁体   English

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

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

I am using a line renderer and a circle png at the end of the line renderer.我在线条渲染器的末尾使用线条渲染器和圆形 png。 When the a raycast hits a collided object, the circle png appears at the end of the line.当光线投射碰到碰撞的 object 时,圆形 png 出现在行尾。 The problem I am facing is;我面临的问题是; if the object is 10 meters away, I am able to see the circle png easily but if the collider is less than 4 meters, the image is too big.如果 object 距离 10 米,我可以轻松看到圆形 png,但如果对撞机小于 4 米,则图像太大。 How can I rescale the image so that if its farther, it stays the same and if its near then it scales down so that it looks similar if its far or near.我如何重新缩放图像,以便如果它更远,它保持不变,如果它接近,那么它会缩小,以便它看起来相似,如果它远或近。

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;
        }
    }
}

UPDATE:更新:

The distance of the hit point can be calculated with the raycast hit point as:命中点的距离可以用光线投射命中点计算为:

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

With this, I would like to rescale GreenPoint_Raycast up/down possibly with Lerp?有了这个,我想用 Lerp 重新调整 GreenPoint_Raycast 的大小?

I think you need to use a simple proportion.我认为你需要使用一个简单的比例。

dist1/scale1 = dist2/scale2

If you say that on 10 meteres the scale is right (assuming that you measure it with your dist variable), we can put those numbers in proportion.如果你说 10 米的比例是正确的(假设你用dist变量测量它),我们可以按比例排列这些数字。

10 / 0.001323662 = dist / scale

so the desired scale = 0.001323662 * dist / 10所以所需的比例= 0.001323662 * dist / 10

Update: well, you deleted the calculation of dist更新:好吧,你删除了dist的计算

But I guess it is Vector3.Distance(hit.point, RightHandAnchor.transform.position)但我猜它是Vector3.Distance(hit.point, RightHandAnchor.transform.position)

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

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