简体   繁体   中英

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. When the a raycast hits a collided object, the circle png appears at the end of the line. 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. 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?

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 / 0.001323662 = dist / scale

so the desired scale = 0.001323662 * dist / 10

Update: well, you deleted the calculation of dist

But I guess it is Vector3.Distance(hit.point, RightHandAnchor.transform.position)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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