There are 2 ways to make mouse interaction in Unity’s First Person Controller:

  1. Make cursor visible;
  2. Use scripting: Raycasters.

1.Make cursor visible

To achieve this, you only need to switch off “Lock Cursor” in the Inspector of FPSController. After you can see the cursor, you will find the delay feeling of the motion of camera and the motion of cursor. To solve this, you need to adjust “X Sensitivity” and “Y Sensitivity”, normally 8 and 8 is good.

屏幕快照 2018-02-20 下午11.41.56

Then you can set up any “void OnMouseDown () {}” script function.

As the example of changing scene, the C# script will be this:

using UnityEngine;
using UnityEngine.SceneManagement;
public class mouseTester : MonoBehaviour
{
    bool doChange;
    void Start()
    {
        doChange = false;
    }
    void Update()
    {
        sceneChange();
    }
    void OnMouseDown()
    {
        doChange = !doChange;
    }
    void sceneChange()
    {
        if (doChange)
        {
            SceneManager.LoadScene(scene2);
        }
    }
}

2.Raycasters

To achieve this, you have to first understand what a Raycaster is in Unity. Here I find a great definition:

ray is a mathematical device that starts at an origin point and continues on in a specific direction forever. With a raycast you’re casting a ray, cast being used like the word throw. It’s like if you threw a rock and it continued on in that direction forever, it wouldn’t stop until it hit something. You’re interested as to whether it hit an object and what that object was.

Blocking objects would be the objects that you specifically define your raycast to be able to hit.
blocking mask allows you to, instead of passing tons of blocking objects, define the layers in Unity you want your raycast to be able to hit in the form of a bitmask. The bitmask is coded so each bit represents a layer. If your bitmask was 0000000000000101 represented as (1 | 1<<2) or 5then your raycast will only be blocked by layers 1 and 3 and can therefore only hit objects in those layers.

In this sample, you are able to cast a red ray, and when you look at an object (in other words, the ray is casting on an object), in the same time, if you press left click you the scene will be changed to scene2.

Feb-21-2018 00-51-22.gif

  1. Drag the script on “FirstPersonCharacter”;
  2. Adjust “Ray Length” in Inspector of “FirstPersonCharacter”;
  3. Make sure the Tag and Name of target object are same as those in the script, in the example, they are “Interactive” and “Cube”;屏幕快照 2018-02-21 上午12.58.47.png
  4. Create a new scene, and the name of it should be same as it in the script, in the example, it is “scene2”;
  5. In the “File>Build Setting”, you need to use “Add Open Scenes” to include both scenes;
  6. Done.
using System.Collections;   
using UnityEngine;
using UnityEngine.SceneManagement;

public class RaySceneChange : MonoBehaviour {
    //RaycastHit defines the name of ray
    private RaycastHit vision;
    public float rayLength;
    bool doChange;
    void Start()
    {
        doChange = false;
    }
    void Update()
    {
        //Casting a red ray. only for understanding how Raycasters work, you can delect this sentence
        //The first two elements in brackets define the direction of the ray. In this example, the direction is in the middle of the screen.
        Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * rayLength, Color.red, 0.5f);
        sceneChange();
        if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out vision, rayLength))
        {
            //Only the object with the defined Tag can be interactived, which is in the Inspector of object
            if (vision.collider.tag == Interactive)
            {
                //nly the object with the defined Name can be interactived, which is in the Inspector of object
                if (vision.collider.name == Cube)
                {
                    //GetMouseButtonDown: 0 = left click; 1 = right click; 2 = middle click)
                    if (Input.GetMouseButtonDown(0))
                    {
                        doChange = !doChange;
                    }
                }
            }
        }
    }
    void sceneChange()
    {
        if (doChange)
        {
            SceneManager.LoadScene(scene2);
        }
    }
}

I also find a great sample here, which allows audience hold the object by pressing E when look at it.

Feb-21-2018 01-03-14.gif

Here is the C# script. You can just replace the last one with it. The process is the same.

using UnityEngine;
using System.Collections;
public class RayCastExample : MonoBehaviour
{
    private RaycastHit vision;
    public float rayLength;
    private bool isGrabbed;
    private Rigidbody grabbedObject;
    void Start()
    {
        isGrabbed = false;
    }
    void Update()
    {
        Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * rayLength, Color.red, 0.5f);
        if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out visionrayLength))
        {
            if(vision.collider.tag == Interactive)
            {
                Debug.Log(vision.collider.name);
                if(Input.GetKeyDown(KeyCode.E)&& !isGrabbed)
                {
                    grabbedObject = vision.rigidbody;
                    grabbedObject.isKinematic = true;
                    grabbedObject.transform.SetParent(gameObject.transform);
                    isGrabbed = true;
                }
                else if(isGrabbed && Input.GetKeyDown(KeyCode.E))
                {
                    grabbedObject.transform.parent = null;
                    grabbedObject.isKinematic = false;
                    isGrabbed = false;
                }
            }
        }
    }
}

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google+ photo

You are commenting using your Google+ account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s