Unity: Object is not being detected by raycast for highlighting

本小妞迷上赌 提交于 2020-01-02 23:14:16

问题


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.

What am I doing wrong?

I didn't change the code from the source provided. I just imported my object file to the scene and added the necessary physics.

All I want to do is highlight my .obj file via 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;
}

回答1:


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:

  1. 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:


    OR
  2. 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




回答2:


There could be several things causing this but here are a few things to check for.

  • Make sure your GameObject has a Collider Component attached to it.
  • Make sure the GameObjects layer is not set to Ignore Raycast.
  • Make sure you are Raycasting form the right camera.

The tutorial seems to be using a default camera for the Raycast, make sure you have a Camera in the scene that has its Tag set to Main Camera.




回答3:


I used your code in a scene with three cubes (room0 to room2) and a plane (flat) and it worked fine.

But your code is bit weird tough, especially the TransformObject class. This class should be a MonoBehaviour (a script) and added as a Component to the same GameObject than your AppRoot script.

You can even make it automatic by using the RequireComponentAttribute on your AppRoot class.

Now then, why isn't your code working in your case ?

  • Did you try to use the debugger while running your code, to see whether SelectObjectByMousePos() and SelectedObject got called at all?
  • It is possible that the material you wanted to use isn't working with your meshes: try using your script with cubes (like I did) instead of your .obj
  • Rewrite your code so that TransformObject is a MonoBehaviour script.
    • If you are concerned about order of execution, see this page : http://docs.unity3d.com/Documentation/Components/class-ScriptExecution.html
  • Check the collision matrix, maybe some layer to layer collision detection are disabled.

edit: does raycasting work with non-convex colliders?



来源:https://stackoverflow.com/questions/20431431/unity-object-is-not-being-detected-by-raycast-for-highlighting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!