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