Mouse movement x-axis locked - c#

I'm having a problem moving my character’s mouse in particular on x-axi.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movimentoRato : MonoBehaviour
{
public float sensibilidade = 100f;
public Transform playerBody;
float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * sensibilidade * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * sensibilidade * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
Mouse x-axis is locked can somebody help me?

float mouseX = Input.GetAxis("Mouse X") * 2.75f;
float mouseY = Input.GetAxis("Mouse Y") * 2.75f;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);

Related

When look up my character moves forwards in unity

I am using this movement code with a box collider in unity (I am quite a beginner)
public class Movement : MonoBehaviour
{
public float speed = 10.0f;
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
transform.Translate(new Vector3(horizontalInput, 0, verticalInput) * Time.deltaTime * speed);
And this mouselook code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mouselook : MonoBehaviour
{
public float sensitivity = 100.0f;
public float clampAngle = 80.0f;
private float rotX = 0.0f;
private float rotY = 0.0f;
void Start()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotX = rot.x;
rotY = rot.y;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotX += mouseY * sensitivity * Time.deltaTime;
rotY += mouseX * sensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
transform.localRotation = localRotation;
}
}
I tried making a different movement code and I was expecting it to let me use wasd to move in a unity puzzle game.
The thing is
transform.Translate(new Vector3(horizontalInput, 0, verticalInput) * Time.deltaTime * speed);
uses local space and since your object is rotated and looking up it will start to move upwards as well.
You could eliminate that by using global space and erase any movement on Y by doing e.g.
var move = transform.rotation * new Vector3(horizontalInput, 0, verticalInput) * Time.deltaTime * speed;
move.y = 0f;
transform.position += move;

Cursor lock state statement is not working in unity

I am following a first person tutorial on brackeys youtube
channel. This is the link:
https://www.youtube.com/watch?v=_QajrabyTJc
The tutorial requires me to lock my cursor but my cursor doesn't lock, I can move anywhere I would like to. The tutorial is a bit outdated so I am not sure if the code is too.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
I have applied the script onto my camera if this information is needed.

Trying to make the mouse move the camera up and down. but the left and right is walking

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour
{
[SerializeField]float mouseSensitivity;
private Transform parent;
Vector2 turn;
// Start is called before the first frame update
void Start()
{
parent = transform.parent;
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
Rotate();
}
void Rotate(){
float mousex = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
parent.Rotate(Vector3.up, mousex);
float mousey = -Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; ///.deltaTime;
parent.Rotate(Vector3., mousey);
// turn.x += Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
// turn.y += Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
// transform.localRotation = Quaternion.Euler(-turn.y, turn.x, 0);
}
}
This is the code for the camera I have written and been on it for like a day but don't know that much about coding so all I see just confuses me. Basically trying to make the camera move up and down with the mouse but it isn't working but the left and right are walking
Try Replace
float mousey = -Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
///.deltaTime;
parent.Rotate(Vector3., mousey);
with
float mousey = -Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
///.deltaTime;
parent.Rotate(Vector3.right, mousey);
or
float mousey = -Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
///.deltaTime;
parent.Rotate(Vector3.forward, mousey);

Brackeys Fps Controller only moves camera up and down

The Problem
I am currently following Brackeys' First Person movement tutorial. However, I'm still stuck on the camera step as unlike most people having camera movement errors, I can only move my camera up and down (as opposed to only left and right).
The Code
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using UnityEngine;
public class mouselook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
(the script is stored in the camera. playerBody links to a cylinder mesh.)
Every time you call transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); you are resetting the y and z of the euler back to 0. I changed the update function to:
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
playerBody.Rotate(Vector3.up * mouseX);
playerBody.Rotate(Vector3.up * mouseX);
}
And I got more expected behavior. I didn't watch the tutorial you are talking about so it may be fixed/explained further on.

Player movement script like in FPS, Unity3d

I have 2 scripts, which make player move like in the FPS game. But it don't move to that direction, to which player are looking - it always move to the same direction, regardless of the direction of the camera..
mouselook.cs
float yRotation;
float xRotation;
float lookSensitivity = 5;
float currentXRotation;
float currentYRotation;
float yRotationV;
float xRotationV;
float lookSmoothnes = 0.1f;
void Update ()
{
yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
xRotation = Mathf.Clamp(xRotation, -80, 100);
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothnes);
currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothnes);
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
}
playermovement.cs
public float walkSpeed = 6.0F;
public float jumpSpeed = 8.0F;
public float runSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= walkSpeed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
make your mouse look like this
float lookSensitivity = 5;
float mouseX,mouseY;
public Transform playerBody;
float xRotation = 0f;
void Update ()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSentivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSentivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
save this and Drag your Player from hierarchy to the camera script playerBody.
Doesn't it need like a requirecomponentTypeof or something like that

Categories