- Home /
Rigidbody 3D Sticking issue
How would I go about writing a code that checks for wall collisions and says hey if the rigidbody hits wall, the player rigidbody can no longer move towards the wall and must move left or right in either axis (x,z).
Also I don't want to use physics material because it will clash with future plans...
-Also im having issues with my player cords going into scientific notation which affects my jumping in some instances.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Player : MonoBehaviour
{
public float movementVelocity;
Vector3 forward;
public float rotationSpeed;
Quaternion rotate;
public float jumpVelocity;
public float fallMultiplier;
public float lowerJumpMultiplier;
private bool isGrounded;
public Rigidbody playerRigidbody;
void Start()
{
playerRigidbody = GetComponent<Rigidbody>();
}
//-----//
void Update()
{
_Jump();
}
//-----//
void FixedUpdate()
{
Move();
_Rotate();
}
//-----//
void Move()
{
forward.x = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
forward.z = Input.GetAxisRaw("Vertical") * Time.deltaTime;
var velocity = new Vector3(forward.x, 0, forward.z).normalized * movementVelocity;
velocity.y = playerRigidbody.velocity.y;
playerRigidbody.velocity = velocity;
}
//-----//
void _Rotate()
{
if (forward != Vector3.zero)
{
rotate = Quaternion.LookRotation(forward, Vector3.up);
playerRigidbody.rotation = Quaternion.RotateTowards(playerRigidbody.rotation, rotate, rotationSpeed);
}
}
//-----//
void _Jump()
// "Better Jump" Provided By: Board To Bits Games //
{
if (Input.GetButtonDown("Jump") && isGrounded)
{
playerRigidbody.velocity = Vector3.up * jumpVelocity;
isGrounded = false;
}
if (playerRigidbody.velocity.y < 0)
{
playerRigidbody.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
else if (playerRigidbody.velocity.y > 0 && !Input.GetButton("Jump"))
{
playerRigidbody.velocity += Vector3.up * Physics.gravity.y * (lowerJumpMultiplier - 1) * Time.deltaTime;
}
}
//-----//
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Ground")
isGrounded = true;
}
}
Answer by Zottic · Jul 10, 2021 at 07:59 PM
Try to add a physics material to the wall with friction near 0.
Also not all walls are walls, some are elevated platforms with side which are acting like walls, but its still labeled as the ground
Answer by iR3dyPlayZ · Jul 10, 2021 at 08:52 PM
Okay so i found code monkeys tutorial for movement and wall detection, but he uses
Raycasthit raycasthit = Physics.Raycast(transform.postion);
and I'm using a rigidbody, but im not able to use
Raycasthit raycasthit = Physics.Raycast(playerRigidbody.velocity = new Vector3(forward.x, 0, forward.z)).normalized * movementVelocity;
Follow this Question
Related Questions
Raycasting in 2D to prevent a player from moving through walls? 1 Answer
Problem with softbodies collisions 0 Answers
How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers
Problem with collision detection during animation 1 Answer
Why can't i addforce to a rigidbody? 2 Answers