C# Linecast在Vector3前停止
我正试图做一个线广播,但它太精确了,所以它没有按照我想要的方式工作 我似乎不知道如何从点C# Linecast在Vector3前停止,c#,unity3d,C#,Unity3d,我正试图做一个线广播,但它太精确了,所以它没有按照我想要的方式工作 我似乎不知道如何从点a进行直线投射,并在远离点B的地方停止0.001f(点a意思是camera.transform.position和点B意思是handle) 那么,从摄影机到点,如何使Linecast在点之前停止 编辑 前视图 在前视图中,并非所有手柄都显示: 后视图 在后视图中,几乎所有手柄都在那里: 完整代码 creatorcreatoreditor.cs using UnityEngine; using UnityE
a
进行直线投射,并在远离点B
的地方停止0.001f
(点a
意思是camera.transform.position
和点B
意思是handle
)
那么,从摄影机到点,如何使Linecast
在点之前停止
编辑
前视图
在前视图中,并非所有手柄都显示:
后视图
在后视图中,几乎所有手柄都在那里:
完整代码
creatorcreatoreditor.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using Creature;
[CustomEditor(typeof(Creator))]
public class CreatureCreatorEditor : Editor {
HashSet<Vector3> handles = new HashSet<Vector3>();
void OnEnable(){
Creator t = (Creator)target;
Mesh mesh = t.GetComponent<MeshFilter>().sharedMesh;
if (mesh != null) {
Vector3[] vertices = mesh.vertices;
Vector3 lp = t.transform.position;
foreach (Vector3 v in vertices) {
Vector3 p = (lp - v);
handles.Add(new Vector3 (p.x, -p.z, p.y));
}
}
}
public void OnSceneGUI(){
Handles.color = Color.red;
Camera camera = Camera.current;
foreach(Vector3 handle in handles){
Vector3 point = camera.WorldToViewportPoint(handle);
if(point.x > 0 && point.x < 1 && point.y > 0 && point.y < 1){
// float dist = Vector3.Distance(camera.transform.position, handle);
// Vector3 fwd = camera.transform.TransformDirection(handle);
Vector3 newBPos = new Vector3(handle.x - 0.001f, handle.y - 0.001f, handle.z - 0.001f);
if(!Physics.Linecast(camera.transform.position, newBPos)){
Handles.FreeMoveHandle(handle, Quaternion.identity, 0.001f, Vector3.zero, Handles.DotCap);
}
}
}
}
public override void OnInspectorGUI(){
}
}
using UnityEngine;
using UnityEditor;
using System.Collections;
namespace Creature{
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshCollider))]
public class Creator : MonoBehaviour {
}
}
你需要一个补偿。您应该从B位置减去0.001f
const float newPosOffset = 0.001f;
foreach(Vector3 handle in handles){
Vector3 newBPos = new Vector3(handle.x-newPosOffset ,handle.y-newPosOffset ,handle.z-newPosOffset );
if(!Physics.Linecast(camera.transform.position, newBPos)){
Handles.FreeMoveHandle(handle, Quaternion.identity, 0.001f, Vector3.zero, Handles.DotCap);
}
}
这将在x,y,z位置上完成
const float newPosOffset = 0.001f;
foreach(Vector3 handle in handles){
Vector3 newBPos = new Vector3(handle.x-newPosOffset ,handle.y-newPosOffset ,handle.z-newPosOffset );
if(!Physics.Linecast(camera.transform.position, newBPos)){
Handles.FreeMoveHandle(handle, Quaternion.identity, 0.001f, Vector3.zero, Handles.DotCap);
}
}
我喜欢它的工作原理,我只需要让Y轴正常工作
public void OnSceneGUI(){
Camera camera = Camera.current;
Handles.color = Color.red;
Vector3 mp = Event.current.mousePosition;
Ray ray = camera.ScreenPointToRay(mp);
RaycastHit hit;
if(Physics.Raycast(ray, out hit)){
Vector3 pos = hit.point;
foreach(Vector3 handle in handles){
if(Vector3.Distance(handle, pos) <= 0.05f){
Handles.FreeMoveHandle(handle, Quaternion.identity, 0.001f, Vector3.zero, Handles.DotCap);
}
}
}
}
public void OnSceneGUI(){
Camera=Camera.current;
Handles.color=color.red;
Vector3 mp=Event.current.mousePosition;
光线=摄影机.屏幕指针阵列(mp);
雷卡斯特击中;
如果(物理.光线投射(光线,出击)){
矢量3位置=命中点;
foreach(句柄中的矢量3句柄){
if(矢量3.距离(手柄,位置)camera.transform.position是点A,handle是点B?是否要将其应用于所有轴?A、y、z?这似乎不起作用。我将在一个重要的位置添加图片。我添加了图片。请看一看,所以您希望所有的handle都像后视图一样显示?是的,无论我以何种视图/角度查看模型,我都不希望它起作用。我要检查我的代码是否有影响。如果用代码替换我的代码,它是否会像后视图一样显示所有内容。我的代码是否有影响?还有什么原因让你认为在0.001f之前停止会使它工作?