Unity:raycast未检测到对象突出显示 [英] Unity: Object is not being detected by raycast for highlighting
问题描述
我遵循了教程有关对象选择.但是,当我导入我的.obj
资产并尝试选择/突出显示它们时,光线投射器似乎没有拾取它们.当我的鼠标单击.obj对象时,什么也没有发生.我添加了必要的对撞机(盒式对撞机甚至网状对撞机),但没有任何反应.
I followed this tutorial on object selection. However, when I import my .obj
assets and try to select/highlight them, it appears that the raycaster does not pick them up. Nothing happens when my mouse clicks on my .obj object. I added the necessary colliders (box collider even mesh collider) and nothing happens.
我做错了什么?
我没有从提供的源中更改代码.我只是将目标文件导入场景,并添加了必要的物理原理.
I didn't change the code from the source provided. I just imported my object file to the scene and added the necessary physics.
我要做的就是通过onMouseDown
突出显示我的.obj
文件.
All I want to do is highlight my .obj
file via onMouseDown
.
AppRoot.cs:
AppRoot.cs:
using UnityEngine;
using System;
public class TransformObject
{
///////////////////////////////////////////////////////////////////////////
#region Variables
// variables
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
private float RotationSpeed = 1500;
private float MoveSpeed = 50.0f;
private float ZoomSpeed = 15.3f;
#endif // UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
public float MinDist = 2.0f;
public float MaxDist = 50.0f;
private Transform mMoveObject = null;
#endregion
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#region Public methods
/// <summary>
///
/// </summary>
public TransformObject()
{
EnabledMoving = true;
}
/// <summary>
/// Sets transform that will be used as "center" of the rotate / pan / zoom
/// </summary>
public void SetTransformRotateAround(Transform goMove)
{
mMoveObject = goMove;
if (mMoveObject == null)
{
Debug.LogWarning("Error! Cannot find object!");
return;
}
}
public void Update()
{
if (!EnabledMoving)
{
return;
}
Vector3 dir = mMoveObject.position - Camera.main.transform.position;
float dist = Math.Abs(dir.magnitude);
Vector3 camDir = Camera.main.transform.forward;
Vector3 camLeft = Vector3.Cross(camDir, Vector3.down);
Vector3 camDown = Vector3.Cross(camDir, camLeft);
//Vector3 camUp = Vector3.Cross(camLeft, camDir);
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
float dx = Input.GetAxis("Mouse X");
float dy = Input.GetAxis("Mouse Y");
// rotate
if (Input.GetMouseButton(0))
{
mMoveObject.Rotate(camLeft, dy * RotationSpeed * Time.deltaTime, Space.World);
mMoveObject.Rotate(Vector3.down, dx * RotationSpeed * Time.deltaTime, Space.Self);
}
// move
if (Input.GetMouseButton(1))
{
Vector3 camPos = Camera.main.transform.position;
camPos += -camLeft * MoveSpeed * dx * Time.deltaTime;
camPos += -camDown * MoveSpeed * dy * Time.deltaTime;
Camera.main.transform.position = camPos;
}
// zoom
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (dist > MinDist)
{
mMoveObject.Translate(-dir * ZoomSpeed * Time.deltaTime, Space.World);
}
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (dist < MaxDist)
{
mMoveObject.Translate(dir * ZoomSpeed * Time.deltaTime, Space.World);
}
}
#endif // UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
}
#endregion
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#region Properties
/// <summary>
/// Gets or set value indicating if transformation is enabled
/// </summary>
public bool EnabledMoving
{
get;
set;
}
/// <summary>
/// Gets game object that moves around
/// </summary>
public Transform MoveObject
{
get
{
return mMoveObject;
}
}
#endregion
///////////////////////////////////////////////////////////////////////////
}
TransformObject.cs
TransformObject.cs
using UnityEngine;
using System;
public class TransformObject
{
///////////////////////////////////////////////////////////////////////////
#region Variables
// variables
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
private float RotationSpeed = 1500;
private float MoveSpeed = 50.0f;
private float ZoomSpeed = 15.3f;
#endif // UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
public float MinDist = 2.0f;
public float MaxDist = 50.0f;
private Transform mMoveObject = null;
#endregion
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#region Public methods
/// <summary>
///
/// </summary>
public TransformObject()
{
EnabledMoving = true;
}
/// <summary>
/// Sets transform that will be used as "center" of the rotate / pan / zoom
/// </summary>
public void SetTransformRotateAround(Transform goMove)
{
mMoveObject = goMove;
if (mMoveObject == null)
{
Debug.LogWarning("Error! Cannot find object!");
return;
}
}
public void Update()
{
if (!EnabledMoving)
{
return;
}
Vector3 dir = mMoveObject.position - Camera.main.transform.position;
float dist = Math.Abs(dir.magnitude);
Vector3 camDir = Camera.main.transform.forward;
Vector3 camLeft = Vector3.Cross(camDir, Vector3.down);
Vector3 camDown = Vector3.Cross(camDir, camLeft);
//Vector3 camUp = Vector3.Cross(camLeft, camDir);
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
float dx = Input.GetAxis("Mouse X");
float dy = Input.GetAxis("Mouse Y");
// rotate
if (Input.GetMouseButton(0))
{
mMoveObject.Rotate(camLeft, dy * RotationSpeed * Time.deltaTime, Space.World);
mMoveObject.Rotate(Vector3.down, dx * RotationSpeed * Time.deltaTime, Space.Self);
}
// move
if (Input.GetMouseButton(1))
{
Vector3 camPos = Camera.main.transform.position;
camPos += -camLeft * MoveSpeed * dx * Time.deltaTime;
camPos += -camDown * MoveSpeed * dy * Time.deltaTime;
Camera.main.transform.position = camPos;
}
// zoom
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (dist > MinDist)
{
mMoveObject.Translate(-dir * ZoomSpeed * Time.deltaTime, Space.World);
}
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (dist < MaxDist)
{
mMoveObject.Translate(dir * ZoomSpeed * Time.deltaTime, Space.World);
}
}
#endif // UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
}
#endregion
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#region Properties
/// <summary>
/// Gets or set value indicating if transformation is enabled
/// </summary>
public bool EnabledMoving
{
get;
set;
}
/// <summary>
/// Gets game object that moves around
/// </summary>
public Transform MoveObject
{
get
{
return mMoveObject;
}
}
#endregion
///////////////////////////////////////////////////////////////////////////
}
Constants.cs:
Constants.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Constants
{
public const float cMaxRayCastDistance = 1000.0f;
}
推荐答案
原因是"femur_left_1_7_dist/default"和"femur_left_1_7_prox/default"没有对撞机.因此,有两种方法可以解决此问题:
The reason was that "femur_left_1_7_dist/default" and "femur_left_1_7_prox/default" has no colliders. So there are two ways to resolve this issue:
-
在项目视图中,选择"femur_left_1_7_dist"和"femur_left_1_7_prox",然后在检查器的导入"设置中选择生成碰撞体",然后选择生成碰撞体".然后按应用"按钮:
In project view select "femur_left_1_7_dist" and "femur_left_1_7_prox", and in inspector in Import settings choose "Generate Colliders" and press "Apply" button:
或
选择"femur_left_1_7_dist/default";在场景中,然后按组件/物理/对撞机";在此处查看结果: https://dl.dropboxusercontent.com/u/20023505/stackoverflow_forum/s_fix.zip
Select "femur_left_1_7_dist/default" in the scene and press "Component/Physics/Box Collider"; see the result here: https://dl.dropboxusercontent.com/u/20023505/stackoverflow_forum/s_fix.zip
这篇关于Unity:raycast未检测到对象突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!