在对象选择方面,我遵循了这个教程。但是,当我导入我的.obj资产并尝试选择/高亮显示它们时,光线传送器似乎不会将它们拾取起来。当鼠标单击我的.obj对象时,什么都不会发生。我增加了必要的对撞机(盒子对撞机,甚至网格对撞机),什么也没有发生。
我做错了什么?
我没有从源代码中更改代码。我只是将我的目标文件导入到场景中,并添加了必要的物理信息。
我想做的就是通过.obj高亮显示我的onMouseDown文件。
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
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:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Constants
{
public const float cMaxRayCastDistance = 1000.0f;
}发布于 2013-12-09 22:45:03
其原因是"femur_left_1_7_dist/default“和"femur_left_1_7_prox/default”没有对撞机。因此,解决这个问题有两种方法:

或
发布于 2013-12-06 19:22:56
造成这种情况的原因可能有几个,但以下是一些需要检查的事情。
本教程似乎是使用默认相机的Raycast,确保您有一个相机在现场,它的标签设置为主相机。
发布于 2013-12-07 10:11:21
我在一个场景中使用了您的代码,其中有三个立方体(room0到room2)和一个平面(平面),它工作得很好。
但是您的代码有点奇怪,特别是TransformObject类。这个类应该是一个MonoBehaviour (脚本),并作为一个组件添加到与AppRoot脚本相同的GameObject中。
您甚至可以通过在RequireComponentAttribute上使用AppRoot类使其自动化。
那么,为什么您的代码在您的情况下不能工作呢?
编辑:用非凸对撞机进行光线投射吗?
https://stackoverflow.com/questions/20431431
复制相似问题