Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question [SOLVED] Problems with multi mesh floors

Discussion in 'Input System' started by LPOIKH, May 7, 2024.

  1. LPOIKH

    LPOIKH

    Joined:
    Mar 15, 2024
    Posts:
    6
    Im having problems with this strange bug where my character wont jump specifically when the bottom of the rigidbody is colliding with multiple objects, while WASD input is occurring. It works while moving on a continuous floor and also while not moving on a segmented floor, and the code doesnt have problems with multiple collisions anywhere else so I have no idea what might be causing this.

    I hope I embedded my code right this time, still new to the forums.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Body : MonoBehaviour
    {
    public Rigidbody rb;
    public GameObject Sphere;
    private int Collisions = 0;
    public float Max_Speed = 1000;
    public float Min_Speed = 100;
    private float Speed;
    public float Dampening = 4;
    private bool Jump_Press;
    public float Height = 100;
    Vector2 Normal;
    void Start()
    {

    }
    void OnCollisionEnter(Collision collision)
    {
    Collisions++;
    }
    void OnCollisionExit(Collision collision)
    {
    Collisions--;
    }
    void Update()
    {
    RaycastHit hit;
    if(Input.GetKeyDown(KeyCode.Space) && Physics.SphereCast(transform.position + Vector3.up * 0.35f, 0.35f, Vector3.down, out hit, 1.25f)){
    Jump_Press = true;
    }
    }
    void FixedUpdate()
    {
    Vector3 Forward = Sphere.transform.forward;
    Vector3 Right = Sphere.transform.right;
    Forward.y = 0;
    Forward.Normalize();
    Normal.y = Input.GetAxisRaw("Vertical");
    Normal.x = Input.GetAxisRaw("Horizontal");
    Normal.Normalize();
    if(Input.GetAxisRaw("Vertical") != 0){
    rb.AddForce(Forward * Speed * Normal.y * Time.deltaTime);
    }
    if(Input.GetAxisRaw("Horizontal") != 0){
    rb.AddForce(Right * Speed * Normal.x * Time.deltaTime);
    }
    if(Input.GetKey(KeyCode.LeftShift)){
    Max_Speed = 1875;
    } else{
    Max_Speed = 1500;
    }
    if(Collisions > 0){
    Speed = Max_Speed;
    } else if(Speed > Min_Speed && Collisions <= 0){
    Speed /= Dampening;
    }
    if(Jump_Press == true){
    rb.velocity = new Vector3(rb.velocity.x, Height, rb.velocity.z);
    Jump_Press = false;
    }
    }
    }


    Any help would be greatly appreciated!
     
  2. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    384
    have you tried debugging your update to tell if you're actually able to jump? it could be that the spherecast is detecting one of the other floors.

    with a spherecast, it includes the size of the sphere in it's check, so it could be hitting the floor or even the ceiling if you're moving it too far upward.

    a wise man once said the best way to code is by sprinkling debug.log liberally in your scripts.
     
  3. LPOIKH

    LPOIKH

    Joined:
    Mar 15, 2024
    Posts:
    6
    Why might the spherecast be returning false while moving on the segmented platform? As far as Im aware number of intersections doesnt matter for spherecasts and why is it only while moving?
     
  4. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    384
    # of intersections doesn't matter as you said, it just returns the first result.

    without more info, i can't really help you.

    try using a layer mask on the spherecast to only detect walkable surfaces, maybe it's hitting your player collider or something else attached to player?

    also, you'll get better performance if you create the raycasthit at start instead of each update.
     
  5. LPOIKH

    LPOIKH

    Joined:
    Mar 15, 2024
    Posts:
    6
    Setting a ground specific layer hasnt seemed to work, what more information should I give to help diagnose this?
     
  6. LPOIKH

    LPOIKH

    Joined:
    Mar 15, 2024
    Posts:
    6
    Ive finally found a solution! I had to subtract the default contact offset from the multiplier on the vector3 up and add it to the radius when defining the spherecast. Im sure its not a perfect solution but it has solved this month long problem.

    if(Input.GetKeyDown(KeyCode.Space) && Physics.SphereCast(transform.position + Vector3.up * (0.35f - Physics.defaultContactOffset), 0.35f + Physics.defaultContactOffset, Vector3.down, out hit, 1.25f, Layer)){
    Jump_Press = true;
    }

    Thanks to POOKSHANK for their help!
     
    POOKSHANK likes this.