我在对象选择上遵循了这个tutorial。但是,当我导入我的.obj
资源并尝试选择/突出显示它们时,看起来raycaster不会选择它们。当我的鼠标点击我的.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;
}
答案 0 :(得分:1)
可能会有几件事情导致这种情况,但这里有一些事情需要检查。
该教程似乎使用了Raycast的默认摄像头,确保场景中的摄像头的标签设置为主摄像头。
答案 1 :(得分:1)
我在三个立方体(room0到room2)和一个平面(平面)的场景中使用了你的代码,它运行良好。
但是你的代码有点怪异,特别是 TransformObject 类。此类应为 MonoBehaviour (脚本),并作为组件添加到与 AppRoot 相同的 GameObject 脚本。
您甚至可以使用 AppRoot 类上的RequireComponentAttribute自动创建。
现在,为什么你的代码不适用于你的情况?
编辑光线投射是否适用于非凸面碰撞器?
答案 2 :(得分:0)
原因是“femur_left_1_7_dist / default”和“femur_left_1_7_prox / default”没有碰撞器。因此,有两种方法可以解决此问题:
在项目视图中选择“femur_left_1_7_dist”和“femur_left_1_7_prox”,然后在导入设置的检查器中选择“生成碰撞器”并按“应用”按钮:
OR
在场景中选择“femur_left_1_7_dist / default”并按“Component / Physics / Box Collider”;在此处查看结果:https://dl.dropboxusercontent.com/u/20023505/stackoverflow_forum/s_fix.zip