There are 2 ways to make mouse interaction in Unity’s First Person Controller:
- Make cursor visible;
- 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.
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:
A 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.
A 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 was0000000000000101
represented as(1 | 1<<2)
or5
then 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.
- Drag the script on “FirstPersonCharacter”;
- Adjust “Ray Length” in Inspector of “FirstPersonCharacter”;
- Make sure the Tag and Name of target object are same as those in the script, in the example, they are “Interactive” and “Cube”;
- Create a new scene, and the name of it should be same as it in the script, in the example, it is “scene2”;
- In the “File>Build Setting”, you need to use “Add Open Scenes” to include both scenes;
- 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.
Here is the C# script. You can just replace the last one with it. The process is the same.