Search Unity

Having some issues with Playercontroller and physics.

Discussion in 'General Discussion' started by robertcourto2016, Nov 19, 2019.

  1. robertcourto2016

    robertcourto2016

    Joined:
    Nov 19, 2019
    Posts:
    1
    Hi, I am very new here and am working on many different tutorials and courses for Unity.

    I have just finished Brackeys FPS Controller tutorial



    and also his Making Your First Level with Probuilder



    And I am finding that implementing the last bit of code from the first video that allows for jumping and checking the isGrounded boolean seems to negate the entire spherecheck-gravity situation. Commenting it out seems to fix parts of the issue, depending on what parts I comment out; but refactoring the check itself doesn't appear to change anything, however.

    Basically when i put in the code to check for spacebar input and then add upward velocity, not only does it not do that but it causes the player agent to not move down on the y-axis at all. As in, you can climb the stairs and walk off the ledge and the player doesn't fall down at all.

    Thank you guys for your time; I will update if I make any more progress this way.

    Current Version: 2019.2.11f1
    OS: Win 10 Pro x64

    My PlayerMovement script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     //Associates the script to the Playercontroller module
    8.     public CharacterController controller;
    9.  
    10.     //Initializes all the basic physics statics
    11.     public float speed = 12f;
    12.     public float gravity = 20f;
    13.     public float jumpHeight = 3f;
    14.  
    15.     //Asking for the Empty Object 'groundCheck'
    16.     public Transform groundCheck;
    17.     public float groundDistance = 0.4f;
    18.     public LayerMask groundMask;
    19.  
    20.     //universal velocity variable
    21.     Vector3 velocity;
    22.     bool isGrounded;
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         //creates a bool based on whether the sphere of influence for the groundCheck
    28.         //empty touches any objects on the Ground layer
    29.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    30.  
    31.         //making sure the player agent is seated firmly
    32.         if (isGrounded && velocity.y < 0)
    33.         {
    34.             velocity.y = -2f;
    35.         }
    36.  
    37.         //asking for keyboard input
    38.         float x = Input.GetAxis("Horizontal");
    39.         float z = Input.GetAxis("Vertical");
    40.  
    41.         // ⌂y = ½g * t^2 || gravity formula
    42.         Vector3 move = transform.right * x + transform.forward * z;
    43.    
    44.         controller.Move(move * speed * Time.deltaTime);
    45.  
    46.         //checking for Jump input
    47.         if (Input.GetButtonDown("Jump") && isGrounded)
    48.         {
    49.             // v = √h * -2 * t^2 || jumping formula
    50.             velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    51.         }
    52.  
    53.         velocity.y -= gravity * Time.deltaTime;
    54.  
    55.         controller.Move(velocity * Time.deltaTime);
    56.     }
    57. }
    58.  
    Screenshot: Capture1.PNG

    Source: https://drive.google.com/open?id=1gaI-wbib0H4DP5Ue216h4SWApH32E_lF
     
    Last edited: Nov 19, 2019
  2. guillex

    guillex

    Joined:
    Dec 7, 2015
    Posts:
    1
    same here, is grounded seems to be broken when using a pro builder object as a floor
     
  3. XSwagger

    XSwagger

    Joined:
    Mar 23, 2020
    Posts:
    1
    I cant jump
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    This has nothing to do with ProBuilder. An object that you create with ProBuilder is no different than any other.

    You need to check the values of groundCheck.position and groundDistance and make sure that they are reasonable. Together these form a sphere. The sphere needs to be just slightly big enough to extend a little below your character's capsule collider so that it can collide with the ground. If your sphere is too small or too high-up, than it won't work.

    Also make sure that the groundMask is not excluding whatever layer your ground is on.
     
    Ryiah likes this.
  5. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Another potential issue is the GroundMask you need to ensure the pro-builder floor items use the same layer as the groundMask.
     
    Ryiah likes this.
  6. jemhop

    jemhop

    Joined:
    Jul 28, 2020
    Posts:
    3
    These two are incorrect, atleast in my case. You have to set the mesh of the ProBuilder stuff to convex. No clue why, but you do. You can change that to be the default in Edit>Preferences>ProBuilder and then under Mesh Settings you can tick Mesh Collider is Convex.

    Annotation 2020-07-28 194432.png
     
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    A non-convex mesh collider should work fine as long as your player's collider is not also a non-convex mesh collider. Collision between two non-convex mesh colliders is not supported.
     
  8. m-9496571

    m-9496571

    Joined:
    Nov 27, 2020
    Posts:
    1
    if im right,you're not and amateur at programming right?if so,please help me,ive never been stuck on a problem longer than this problem so far.my groundCheck wont work(there are no errors,ive saved,restarted Unity,chose layers to ground already) my platform is a terrain but idk if that matters.

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

    public class PlayerMovement : MonoBehaviour
    {
    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -9.81f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    // Update is called once per frame
    void Update()
    {
    isGrounded = Physics.CheckSphere(groundCheck.position,groundDistance,groundMask);

    if(isGrounded && velocity.y < 0)
    {
    velocity.y = -2f;
    }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    velocity.y -= gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);
    }
    }