Having some issues with Playercontroller and physics.

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:

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

public class PlayerMovement : MonoBehaviour
{
    //Associates the script to the Playercontroller module
    public CharacterController controller;

    //Initializes all the basic physics statics
    public float speed = 12f;
    public float gravity = 20f;
    public float jumpHeight = 3f;

    //Asking for the Empty Object 'groundCheck'
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    //universal velocity variable
    Vector3 velocity;
    bool isGrounded;

    // Update is called once per frame
    void Update()
    {
        //creates a bool based on whether the sphere of influence for the groundCheck
        //empty touches any objects on the Ground layer
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        //making sure the player agent is seated firmly
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        //asking for keyboard input
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        // ⌂y = ½g * t^2 || gravity formula
        Vector3 move = transform.right * x + transform.forward * z;
    
        controller.Move(move * speed * Time.deltaTime);

        //checking for Jump input
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            // v = √h * -2 * t^2 || jumping formula
            velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
        }

        velocity.y -= gravity * Time.deltaTime;

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

Screenshot:

Source: https://drive.google.com/open?id=1gaI-wbib0H4DP5Ue216h4SWApH32E_lF

same here, is grounded seems to be broken when using a pro builder object as a floor

I cant jump

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.

1 Like

Another potential issue is the GroundMask you need to ensure the pro-builder floor items use the same layer as the groundMask.

1 Like

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.

6142869--670467--Annotation 2020-07-28 194432.png

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.

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);
}
}