- Home /
Question by
OatMilkLover · Oct 26, 2020 at 11:22 AM ·
physicsplayerprogrammingdetectiongrounded
Raycast not detecting ground after rotating
,I used a raycast to detect whether or not the player, first person by the way, was touching the ground and it worked. However, whenever I move my mouse (to rotate), the console tells me that I am not touching the ground.
Jump script:
// Get player's rigidbody
void Start()
{
rb = GetComponent<Rigidbody>();
}
// You might as well jump
void Update()
{
Debug.Log(Physics.Raycast(transform.position, Vector3.down, distToGround));
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
// Check if player is grounded using raycast
bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, distToGround);
}
Player movement:
// Set speed and player's rigidbody
void Start()
{
speed = 12f;
rb = GetComponent<Rigidbody>();
}
// Movement
void Update()
{
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
Vector3 moveBy = transform.right * x + transform.forward * z;
rb.MovePosition(transform.position + moveBy.normalized * speed * Time.deltaTime);
}
And the look around script:
// Lock and hide cursor
void Start()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
// Rotate camera with mouse
void Update()
{
float x = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
float y = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime * -1f;
transform.Rotate(0f, x, 0f);
headRotation += y;
headRotation = Mathf.Clamp(headRotation, -headRotationLimit, headRotationLimit);
cam.localEulerAngles = new Vector3(headRotation, 0f, 0f);
}
I'm new to raycasts and I'm not sure if it may be detecting the capsule I am using as the player. I would like to know how to fix this. Thank you in advance.
Edit: nvm I just needed to add a +0.1f;
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Ground Check In Unity3D 2019.4? 1 Answer
fluid detection for player in complex shape 0 Answers
2D Character Movement Collision Problem 0 Answers
Punching a crate 0 Answers