I have a player object which is parent to a cylinder object (simply for sprite), MainCamera object, and a checkGround object so that I can check if it is touching the ground in order to jump. The following code is my playermovement code and aside from the fact that it skips in certain conditions, it is working completely fine. I also have a mouseMovement script (attached below) that allows the first person camera rotation (which actually rotates the player object itself). I have clamped the rotation from -45 to 45 degrees on y axis, so that the player can look up the sky or down the ground. Whenever I rotate the camera to a certain agree (and the player rotation hits a certain number), if I move the opposite way I’m leaning to, the object starts skipping (jumping slightly). And if I jump while skipping like that, the object jumps (I suppose) double the amount of jump height. And the vice versa occurs: My jump actions on the way I’m leaning on are hindered, I suppose, half the amount of my jump height.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 0f;
public float gravity = -9.81f * 2;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.5f;
public LayerMask groundMask;
Vector3 velocity;
public bool isGrounded;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouseMovement : MonoBehaviour
{
public float mouseSensitivity = 500f;
float xRotation = 0f;
float yRotation = 0f;
public float groundDistance = 0.8f;
public Transform groundCheck;
Vector3 initialGroundCheckLocalPosition;
Vector3 groundCheckOffset;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
groundCheckOffset = groundCheck.position - transform.position;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
// yukarı aşağı bakma mekaniği
xRotation -= mouseY;
// bakış açısının dönüş sınırını belirlemek için (sonsuza kadar yukarı veya aşağı bakmamak için)
xRotation = Mathf.Clamp(xRotation, -45f, 45f);
// sağa sola bakma mekaniği
yRotation += mouseX;
transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
AdjustGroundCheckPosition();
}
void AdjustGroundCheckPosition()
{
Vector3 adjustedPosition = transform.position + transform.rotation * initialGroundCheckLocalPosition;
groundCheck.position = adjustedPosition;
}
}
I consulted ChatGPT and tweaked a few things but they didn’t work. I tampered with and sometimes reset the offset, but didn’t work either. It advised me to make smoothing factor, which it also provided the code of, but didn’t work. What should I do? (Note: I’m a beginner level game developer and I would appreciate if you gave a brief explanation when you use terms.)
Berke Aytan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.