Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Player walks through stairs in build, but walks up them like it should in the Unity editor

Discussion in 'Scripting' started by Joasdekeizer, May 8, 2024.

  1. Joasdekeizer

    Joasdekeizer

    Joined:
    Sep 10, 2023
    Posts:
    4
    In my Unity editor when i walk on stairs it just goes up the stairs but when I play the build game the player walks through the stairs. Do any of you know how to fix this? This is the script I use for walking up the stairs.

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

    public class StairClimb : MonoBehaviour
    {
    Rigidbody rigidBody;
    [SerializeField] GameObject stepRayUpper;
    [SerializeField] GameObject stepRayLower;
    [SerializeField] float stepHeight = 0.12f;
    [SerializeField] float stepSmooth = 2f;

    private void Awake()
    {
    rigidBody = GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
    stepClimb();
    }

    void stepClimb()
    {
    RaycastHit hitLower;
    if (Physics.Raycast(stepRayLower.transform.position, transform.TransformDirection(Vector3.back), out hitLower, stepHeight))
    {
    if (hitLower.collider.CompareTag("Stairs")) // Controleer of het object dat geraakt wordt de tag "Stairs" heeft
    {
    RaycastHit hitUpper;
    if (!Physics.Raycast(stepRayUpper.transform.position, transform.TransformDirection(Vector3.back), out hitUpper, stepHeight))
    {
    // Als de onderste raycast een trede detecteert en de bovenste niet, en het object heeft de tag "Stairs",
    // dan verplaatst het Rigidbody omhoog met de hoogte van een trede
    rigidBody.position += new Vector3(0f, stepHeight, 0f);
    }
    }
    }
    }
    }
    "
    In the video I show what happens in the Unity editor vs in the build game:
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,154
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/


    This is likely problematic:

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    This means you may not change transform.position, transform.rotation, you may not call transform.Translate(), transform.Rotate() or other such methods, and also transform.localScale is off limits. You also cannot set rigidbody.position or rigidbody.rotation directly. These ALL bypass physics.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
  3. Joasdekeizer

    Joasdekeizer

    Joined:
    Sep 10, 2023
    Posts:
    4
    Thanks but how would I do that? im new to this so i don't know a lot about coding
     
  4. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Also, make sure objects that you want included in the build do not have the "Editor Only" tag assigned.
    If the stair colliders have "Editor Only" tag, they won't be included in the build.
     
  5. Joasdekeizer

    Joasdekeizer

    Joined:
    Sep 10, 2023
    Posts:
    4
    i also have this code, could this one be the problem?

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class PlayerController : MonoBehaviour
    8. {
    9.     public float moveSpeed = 1.4f;
    10.     public float jumpForce = 4f;
    11.     private Rigidbody rb;
    12.     private bool isGrounded;
    13.     private Transform playerTransform; // Toegevoegd om de speler te draaien
    14.  
    15.    
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.         playerTransform = GetComponent<Transform>(); // Toegevoegd om de speler te draaien
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         // Beweging naar links en rechts
    25.         float moveInput = Input.GetAxisRaw("Horizontal");
    26.         rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
    27.  
    28.         // Draai de speler op basis van de bewegingsrichting
    29.         if (moveInput < 0) // Als de speler naar links beweegt
    30.         {
    31.             playerTransform.rotation = Quaternion.Euler(0, 90, 0); // Draai de speler 90 graden
    32.         }
    33.         else if (moveInput > 0) // Als de speler naar rechts beweegt
    34.         {
    35.             playerTransform.rotation = Quaternion.Euler(0, 270, 0); // Draai de speler 270 graden
    36.         }
    37.  
    38.         // Springen
    39.         if (Input.GetKeyDown(KeyCode.UpArrow))
    40.         {
    41.             Jump();
    42.         }
    43.     }
    44.        
    45.     void Jump()
    46.     {
    47.         rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    48.     }
    49.    
    50.  
    51. }
    52.  
     
  6. Joasdekeizer

    Joasdekeizer

    Joined:
    Sep 10, 2023
    Posts:
    4
    i just tried making the stair collider with a capsule collider and that worked, then i duplicated that one and tried again with the mesh collider like i had before and it just worked suddenly