[英]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.