A
A
Alexander123452021-11-27 15:29:44
C++ / C#
Alexander12345, 2021-11-27 15:29:44

How to properly set cursor control in unity 3d?

Everyone knows how the twist works in first-person shooters? Move the mouse, the camera pans. I read on the Internet about how to implement it. Eventually I found this:

using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
 public enum RotationAxes {
 MouseXAndY = 0,
 MouseX = 1,
 MouseY = 2
 }
 public RotationAxes axes = RotationAxes.MouseXAndY;
 public float sensitivityHor = 9.0f;
 public float sensitivityVert = 9.0f;
 public float minimumVert = -45.0f;
 public float maximumVert = 45.0f;
 private float _rotationX = 0;
 void Start() {
 Rigidbody body = GetComponent<Rigidbody>();
 if (body != null)
 body.freezeRotation = true;
 }
 void Update() {
 if (axes == RotationAxes.MouseX) {
 transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);
 }
 else if (axes == RotationAxes.MouseY) {
 _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
 _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
 float rotationY = transform.localEulerAngles.y;
 transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
 }
 else {
 _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
 _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
 float delta = Input.GetAxis("Mouse X") * sensitivityHor;
 float rotationY = transform.localEulerAngles.y + delta;
 transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
 }
 }
}

In principle, it works ... but it's not quite what I need. How can I make a "dot" in the center of the screen, which will act as a mouse pointer? Just started playing with unity, so don't be too hard on yourself.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergo Zar, 2021-11-27
@Alexander12345

In ui, you select a picture, shove a picture of a dot there, and put it in the center of the screen or where you need it. Then remove the cursor

Cursor.visible  = false; // скрыть курсор
Cursor.lockState = CursorLockMode.Locked; // "заморозить" курсор в одном месте
<code>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question