Keep cursor centered with allowed rotation on 3rd person shooter?,How to get camera to follow cursor with cursor always centered?
I am working on a game that's a 3rd person shooter. I'm playing around with the camera in the hopes of making it better. My hope is to make it so that the cursor is always center (as it's a crosshair) and that the camera rotates based on mouse input to always keep the cursor center. Right now I have both a crosshair and a camera that rotates to meet the crosshair, the problem is the crosshair isn't kept center. The camera code is using System.Collections; using System.Collections.Generic; using UnityEngine; public class RotationCameraDrag : MonoBehaviour { public float dragSpeed = 100; private Vector3 dragOrigin; private Vector3 prevDragOrigin; public GameObject goToRotate; public bool dragYaw = false; public Camera mycam; void Update() { prevDragOrigin = dragOrigin; dragOrigin = Input.mousePosition; dragSpeed = (dragOrigin.magnitude / prevDragOrigin.magnitude)/(Time.deltaTime*40); Vector3 pos = Camera.main.ScreenToViewportPoint((Input.mousePosition - new Vector3(mycam.pixelWidth/2, mycam.pixelHeight/2, 0))); Vector3 move = new Vector3(0,pos.x * dragSpeed, 0); goToRotate.transform.Rotate(move, Space.Self); if (dragYaw) { gameObject.transform.Rotate(new Vector3(pos.y * dragSpeed, 0, 0), Space.World); } } }
The crosshair script is just Cursor.visible = false;
on Start() and this.transform.position = Input.mousePosition;
on Update(). I saw some answer that says to do transform.LookAt(mycam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, mycam.nearClipPlane)), Vector3.up);
but this does not work for me, the camera rotates far too aggressively and you just spin in circles really fast. Any recommendations?
Turns out I accidentally duplicated this question, the moderators can delete one.
Your answer
Follow this Question
Related Questions
camera doesn't work after reloading the scene 0 Answers
Is it possible to change the alpha of an object in relation to camera position? 0 Answers
I cannot move my camera in the Game mode 0 Answers
Locking Camera's rotation on a rolling character 0 Answers
Camera controls not working quite right 0 Answers