My Character Keeps Going Diagonal when i start the Unity Game - unity3d

So, I'm making a Survival Game in First Person and this is my code for the Player. But the error is the layer keeps laying down. Even tho there is no gravity. Here is my code:
using UnityEngine;
public class Movement : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
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);
}
}

In your Mathf.Clamp function you use -90 two times, thus setting the xRotation to -90 every time. You propably want to Clamp it between -90 and 90.
Edit: xRotation is reduced using mouseY every frame, but it is never reset. This means, that moving the mouse to the right will cause the camera to continue rotating even after the mouse has stopped moving. You propably want to set xRotation using -mouseY (or plus? not sure) instead of reducing it by mouseY. This should make the camera stop spinning while the mouse is not moved.

Related

Player rotation speed different in Editor and Game Build

The rotation speed of my character is different while testing in the editor and in Built version of the game. By a pretty significant amount. This is the script in question.
using UnityEngine;
public class PlayerCam : MonoBehaviour
{
public float SensX;
public float SensY;
public Transform orientation;
float xRotation;
float yRotation;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * SensX;
float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * SensY;
yRotation += mouseX;
xRotation -= mouseY;
orientation.rotation = Quaternion.Euler(0, yRotation, 0);
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
}
}
If I stop multiplying it by Time.deltaTime it seems to work fine but then I cannot stop the rotation from happening when I want to pause the game in menu's using Time.timeScale = 0
If I change it from Update() to FixedUpdate() the rotation is extreemly jittery and doesn't work at all
Sorry for what is probably a dumb mistake but I don't understand what I am doing wrong.

CHARACTER ROTATION depending on camera IN UNITY

I'm making a 3rd person controller in unity and i'm stuck: i can't figure out how to make the player follow my freelook cinemachine.
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
public GameObject Hoverboard_script;
public GameObject cinemachine_Hoverboard;
public Transform main_cam_tf;
void Update()
{
Hoverboard_script.SetActive(false);
cinemachine_Hoverboard.SetActive(false);
PlayerMovement_void();
CamController_void();
}
void PlayerMovement_void()
{
float hor = Input.GetAxisRaw("Horizontal");
float ver = Input.GetAxisRaw("Vertical");
Vector3 playerMovement = new Vector3(hor, 0f, ver).normalized * speed * Time.deltaTime;
transform.Translate(playerMovement, Space.Self);
}
void CamController_void()
{
float MouseY = main_cam_tf.eulerAngles.y;
Debug.Log(MouseY);
Quaternion rotation = Quaternion.Euler(0f, MouseY, 0f);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * 4f);
}
}
Right now if i rotate the camera, the player enters a non-stop loop where it rotate constantly. How can i solve? Thanks, cheers
If you want to rotate the player as the rotation value of Y of Camera then remove all the lines of codes in which they rotate your object. Instead use one of these:
1.
transform.rotation = Quaternion.AngleAxis(Camera.Main.transform.eulerAngles.y, Vector3.up);
transform.rotation = Quaternion.EulerAngles(transform.eulerAngles.x, Camera.Main.transform.eulerAngles.y, transform.eulerAngles.z);

How can I make an objects rotatable only for the player?

the problem is that if the player touches something that rotate, the player automatically starting rotating too because the object that is rotation right now pushed the player's colliders and and player starting rotating. I want to make my player un rotatable to another objects in the game but rotatable if the player rotate by himself
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camera : MonoBehaviour
{
public enum RotationAxis
{
MouseX = 1,
MouseY = 2
}
public RotationAxis axes = RotationAxis.MouseX;
public float minimumVert = -45.0f;
public float maximumVert = 45.0f;
public float sensHorizontal = 10.0f;
public float sensVertical = 10.0f;
public float _rotationX = 0;
// Update is called once per frame
void Update()
{
if (axes == RotationAxis.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensHorizontal, 0);
}
else if (axes == RotationAxis.MouseY)
{
_rotationX -= Input.GetAxis("Mouse Y") * sensVertical;
_rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert); //Clamps the vertical angle within the min and max limits (45 degrees)
float rotationY = transform.localEulerAngles.y;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
}
}
You could store the rotation in a field and thus making sure the only thing changing it is your script:
move it to LateUpdate in order for it to be the last thing called in a frame (also see Order of Execution for Event Functions)
float xRotation;
float yRotation;
void Start()
{
xRotation = transform.localEulerAngles.x;
yRotation = transform.localEulerAngles.y;
}
void LateUpdate()
{
if (axes == RotationAxis.MouseX)
{
yRotation += Input.GetAxis("Mouse X") * sensHorizontal;
}
else if (axes == RotationAxis.MouseY)
{
xRotation -= Input.GetAxis("Mouse Y") * sensVertical;
//Clamps the vertical angle within the min and max limits (45 degrees)
xRotation= Mathf.Clamp(lastXRotation , minimumVert, maximumVert);
}
// always overwrite with fixed values instead of using transform.rotation based ones
transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0);
}
just to be sure you could also additionally but it to FixedUpdate in order to also reset changes from the Physics. If there is any RigidBody in play this should be the way to go anyway but then not using the Transform but rather the Rigidbody component.
void FixedUpdate()
{
transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0);
}
Note however: As you currently have it setup you will get unexpected rotations. Rotating around the two axis here has the problem of also rotating along the local coordinate system.
In general you should rather have a parent object for the Y rotation and only do the X rotation on the object itself.

Unity3d - How to walk throught streets

I have a 3d city model. I want to walk throught walls/steets. There are some advices for this.
Ex1, ex2, ex3
Generally, they said that you should add a collider to your model. I did it. But I don't know next step. Should I move my camera or player gameobject? Should I handle click movement key in code?
I attach below code to my camera. It works for me. I take this code from this tutorial.
public float speed = 10.0f;
public float rotationSpeed = 100.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float translation = Input.GetAxis ("Vertical") * speed;
float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
}

3rd person camera follower in Unity3D

I try to make a 3rd person camera, which follows my player and the camera should rotate, but not the player, if I use the right analog Stick of my controller. I followed this tutorial
My code:
void adjustCameraToPlayer()
{
Quaternion rotation = Quaternion.identity;
if (Input.GetAxis("RightStickX") != 0f)
{
float horizontal = Input.GetAxis("RightStickX") / 100f;
transform.Rotate(0, horizontal, 0);
float desiredAngle = transform.eulerAngles.y;
rotation = Quaternion.Euler(0, desiredAngle, 0);
}
transform.position = player.transform.position-(rotation * offset);
transform.LookAt(player.transform);
}
My problem is that the camera rotates way too fast, I tried to change the dividend of the horizontal value but it did not help.
That's why you always should incorporate deltaTime into transform operations that happen every frame. That way you aren't rotating it at magnitude every single frame, but instead over time. Also you should incorporate a speed variable which can be manipulated in real time, so you can tweak it just how you want:
public float speed = 5f;
void adjustCameraToPlayer()
{
Quaternion rotation = Quaternion.identity;
if (Input.GetAxis("RightStickX") != 0f)
{
float horizontal = Input.GetAxis("RightStickX");
transform.Rotate(Vector3.up * horizontal * speed * Time.deltaTime);
float desiredAngle = transform.eulerAngles.y;
rotation = Quaternion.Euler(0, desiredAngle, 0);
}
transform.position = player.transform.position-(rotation * offset);
transform.LookAt(player.transform);
}