- Home /
My Character Jumps Higher Than It Should Be When Collides Into a Wall
As I said in the title, my problem is about the physics.
I'm trying to make a first person controller on my on. I can jump like one or two unit with my controller, but when I try to run into a wall or some object and hit jump, my character jumps like 0.2 units. But if I hit jump multiple times, my character can jump lots of higher, like 10 - 12 units. I tried to use a physics material to both my obstacles, floor and player with this settings:
After giving this, problem solved. But with this materials, if I try to climb a 30 degree ramp my character slides back and keeps sliding forever, even it is on straight floor. And another problem is jumping onto objects gives the same sliding-forever effect.
So, what should I do? I don't really wanted to use "Character Controller Component" because I wanted to make everything on my own. Must I use it?
Here is my code:
using UnityEngine;
public class Player : MonoBehaviour
{
[Header("Values")]
public float speed;
public float jumpThrust;
public float cameraSensitivity;
private Vector2 cameraAxis;
private float horizontalAxis;
private float verticalAxis;
private Vector3 moveRotation;
private float cameraY;
[Space(20)]
[Header("Statements")]
public bool isGrounded;
public bool canMove;
public bool canLook;
public bool isMenuActive;
public bool isInventoryActive;
[Space(20)]
[Header("Assigments")]
public PhysicMaterial[] physicMaterials;
private Rigidbody rbody;
private AudioSource aSource;
private GameObject playerCamera;
private Collider playerCollider;
private GameObject playerInterface;
private GameObject inventoryObject;
private GameObject menuObject;
private Animator menuAnimator;
private Animator inventoryAnimator;
void Awake ()
{
rbody = gameObject.GetComponent<Rigidbody>();
aSource = gameObject.GetComponent<AudioSource>();
playerCamera = gameObject.transform.Find("PlayerCamera").gameObject;
playerCollider = gameObject.GetComponent<CapsuleCollider>();
playerInterface = GameObject.FindGameObjectWithTag("PlayerInterface");
inventoryObject = GameObject.Find("Inventory").gameObject;
menuObject = GameObject.Find("Menu").gameObject;
inventoryAnimator = inventoryObject.GetComponent<Animator>();
menuAnimator = menuObject.GetComponent<Animator>();
}
private void Update()
{
if (Input.GetButtonDown("OpenInventory"))
{
if (isMenuActive)
{
canLook = true;
canMove = true;
}
isMenuActive = false;
menuAnimator.SetBool("isMenuActive", false);
isInventoryActive = !isInventoryActive;
inventoryAnimator.SetBool("isInventoryActive", isInventoryActive);
canLook = !isInventoryActive;
canMove = !isInventoryActive;
Debug.Log("Current Inventory statement: " + isInventoryActive);
}
if (Input.GetButtonDown("OpenMenu"))
{
if (isInventoryActive)
{
canLook = true;
canMove = true;
}
isInventoryActive = false;
inventoryAnimator.SetBool("isInventoryActive", false);
isMenuActive = !isMenuActive;
menuAnimator.SetBool("isMenuActive", isMenuActive);
canLook = !canLook;
canMove = !canMove;
Debug.Log("Current Menu statement: " + isMenuActive);
}
if (canLook)
{
CameraMovement();
}
if (canMove)
{
if (Input.GetButtonDown("Jump"))
{
if (isGrounded)
{
Jump();
}
}
}
if (isGrounded)
{
playerCollider.material = physicMaterials[1];
}
else
{
playerCollider.material = physicMaterials[0];
}
}
void FixedUpdate ()
{
if (canMove)
{
Movement();
}
}
void Movement()
{
horizontalAxis = Input.GetAxis("Horizontal");
verticalAxis = Input.GetAxis("Vertical");
moveRotation = new Vector3(horizontalAxis, 0, verticalAxis);
moveRotation = transform.TransformDirection(moveRotation);
moveRotation = Vector3.Normalize(moveRotation);
rbody.MovePosition(transform.position + moveRotation * Time.deltaTime * speed);
}
void CameraMovement()
{
cameraAxis = new Vector2(Input.GetAxis("LookX"), Input.GetAxis("LookY"));
cameraY -= cameraAxis.y * cameraSensitivity * Time.deltaTime;
cameraY = Mathf.Clamp(cameraY, -75, 75);
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y + cameraAxis.x * Time.deltaTime * cameraSensitivity, transform.localEulerAngles.z);
playerCamera.transform.localEulerAngles = new Vector3(cameraY, playerCamera.transform.localEulerAngles.y, playerCamera.transform.localEulerAngles.z);
}
void Jump()
{
RaycastHit ray;
/*if(Physics.Raycast(transform.position, Vector3.down, out ray, 0.3f))
{
}
*/
rbody.AddForce(transform.up * jumpThrust);
}
private void OnTriggerStay(Collider trigger)
{
isGrounded = true;
}
private void OnTriggerExit(Collider trigger)
{
isGrounded = false;
}
}
I'm no expert, but I'm pretty sure you want to add static and dynamic friction if you're using physics where static friction > dynamic friction.
Your answer
Follow this Question
Related Questions
Affecting FPS controller/Character Motor physics with game object 1 Answer
Enemies mess up force controlled Player 2 Answers
Problem with Colliders 1 Answer
First Person Prefab Stopping? 1 Answer