- Home /
I was trying to have the camera track the mouse and this happened what is wrong???
Vid is bad quality but you can notice when I move the mouse the screen shakes I want the camera to rotate with the mouse. Vid: https://youtu.be/KNPOERVCYrU
code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class turn : MonoBehaviour { // Start is called before the first frame update public float sens = 1.0f;
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * sens;
float mouseY = Input.GetAxis("Mouse Y") * sens;
transform.rotation = Quaternion.Euler(mouseX, mouseY, 0);
mouseX *= Time.deltaTime;
mouseY *= Time.deltaTime;
}}
Answer by The_Three_Vs · Mar 31, 2020 at 10:54 PM
As it currently is, your code sets the camera's rotation to be the value of the input axes themselves, so the camera's rotation will always be only minscule rotations in any direction. In order to get the camera to pan with the mouse, you have to make the rotation additive, for example with transform.eulerAngles += new Vector3(-mouseY, mouseX, 0);
(The rearranged mouse values make the camera rotate correctly, as well).
this worked well thx so much just wondering I never figured out what vector3 means?
Furthermore, the mouse input axes only register values when you are actively moving the mouse, not when the mouse is still but off-center. If you want the camera to rotate when the camera is off-center, you could use something like:
Vector2 mousePosition = Input.mousePosition;
float mouseX = $$anonymous$$athf.Sign(mousePosition.x) * sens;
float mouseY = $$anonymous$$athf.Sign(mousePosition.y) * sens;
$$anonymous$$athf.Sign returns -1 if the value is negative, 0 if the value is 0, and +1 if the value is positive.
Vector3 is an object consisting of an x number, a y number, and a z number, all of which convey a distance from a certain origin. For example, a gameObject in Unity with a position of Vector3(5, -3, -8) would be located 5 units to the right, 3 units down, and 8 units behind the world origin. Vector3s are typically used for location, like in the example, but they can be used in any situation where you want a tight set of three float values. With eulerAngles, the provided Vector3 affects what the transform rotation values in the Unity Editor are.
uhh I cant camera to stop when is stop moving mouse.
Whoops. Forgot that the origin of Input.mousePosition is at the bottom left corner of the screen, not the center. Change them to $$anonymous$$athf.Sign(mousePosition.x - (Screen.width / 2))
and $$anonymous$$athf.Sign(mousePosition.y - (Screen.height / 2))
. Alternatively / Additionally, if you want to have a "dead zone" in the middle of the screen, establish a size and then check two "rectangles" on the screen to see if the camera can rotate:
public float deadZoneScreenProportion = 0.1f;
void Update()
{
//Recenters mouse position to be relative to the center of the screen
Vector2 mousePosition = Input.mousePosition;
float xCenter = Screen.width / 2;
float yCenter = Screen.height / 2;
mousePosition -= new Vector2(xCenter, yCenter);
Vector3 cameraRotation = Vector3.zero;
//Dynamically adjusts the deadZoneSize to be relative to the screen so that window resizing won't affect anything
float deadZoneXSize = Screen.width * deadZoneScreenProportion;
float deadZoneYSize = Screen.height * deadZoneScreenProportion;
//If mousePosition.x is outside the vertical dead zone, add rotation
if ($$anonymous$$athf.Abs(mousePosition.x) > deadZoneXSize)
{
cameraRotation.y = $$anonymous$$athf.Sign(mousePosition.x);
}
//If mousePosition.y is outside the horizontal dead zone, add rotation
if ($$anonymous$$athf.Abs(mousePosition.y) > deadZoneYSize)
{
cameraRotation.x = -$$anonymous$$athf.Sign(mousePosition.y);
}
transform.eulerAngles += cameraRotation;
}
Answer by Codester95 · Mar 31, 2020 at 09:10 PM
Take a look at the FreeLookCamera from Unity's standard assets package. You need to have a pivot point for your camera to rotate around. For example you can target the player and rotate in reference to the players transform. If you are still having troubles. Message me and I can help further :)
Answer by bscohen06 · Mar 31, 2020 at 09:25 PM
@codester95 how could I target a player?
I actaully meant the caera rotating not on a axis but just rotating (im tsting for custom player controller)
Your answer
