my ball isn’t jumping normally, it just instantly changes it’s position in mid-air like gravity isn’t applied to it at all, any idea why it isnt working?
problem:ui77i9
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour
{
// jump event system(if jump button is pressed)
public JumpButtonEvent jumpButtonEvent;
//character controller
public CharacterController playerController;
public Collider playerCollider;
public Vector3 playerVector;
public float gravityDown;
public float neutralGravity;
public float movementSpeed = 15f;
public float defaultSpeed = 15f;
public float jumpForce = 50f;
public bool isJumping = false;
public FixedJoystick joystickMove;
float x;
float y;
float z;
public const float gravity = -5;
// public Rigidbody playerRb;
// private Vector3 movement;
public Transform gameCamera;
// raycast hit ground
public bool isGrounded = false;
RaycastHit hitInfo;
RaycastHit slopeHit;
Vector3 raycastDirection;
public LayerMask raycastLayers;
public float sphereRadius = 0.1f;
public float castDistance = 0.01f;
public float slopeCastDistance = 1f;
public float slopeSpeed = 1f;
Vector3 slopeMoveDirection;
Vector3 slopeJumpDirection;
private void Awake()
{
playerController.detectCollisions = true;
raycastDirection = Vector3.down;
// playerRb = GetComponent<Rigidbody>();
playerCollider = GetComponent<SphereCollider>();
}
// Start is called before the first frame update
void Start()
{
}
public void Movement()
{
x = joystickMove.Horizontal;
y = verticalMovement;
z = joystickMove.Vertical;
playerVector = new Vector3(x, y, z); // the playervector input will have the x and z values. y is added later
playerVector = playerVector * movementSpeed; // multiplies the playervector with the movementspeed variable;
playerController.Move(playerVector);
Debug.Log(y);
}
public void Jumping()
{
if (isJumping == false && IsGrounded() == true && jumpButtonEvent.jumpBtnClicked == true) // if it meets the 3 requirements, the player will jump
{
neutralGravity = neutralGravity + jumpForce;
}
/* if (isJumping == false && IsGrounded() == true && OnSlope() == true)
{
slopeJumpDirection = Vector3.ProjectOnPlane(Vector3.up, slopeHit.normal); // detects the normal vector of the surface player is on
playerRb.AddForce(slopeJumpDirection * jumpForce, ForceMode.Impulse); // makes the player jump on the normal direction
} */
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
Jumping();
Movement();
IsGrounded();
// OnSlope();
}
private void MovementButtonRelease()
{
if (joystickMove.Horizontal == 0 && joystickMove.Vertical == 0) // se largar os botoes que mexem o player, a velocidade do rigidbody pára (o player pára de se mexer)
{
}
}
public bool IsGrounded() // cria uma funçao bool que verifica se o player está no chão ou não
{
if (Physics.SphereCast(playerCollider.bounds.center, sphereRadius, raycastDirection, out hitInfo, castDistance, raycastLayers))
{
if (hitInfo.collider.gameObject.layer == 7 || hitInfo.collider.gameObject.layer == 8) // se o layer do objeto que o raycast bateu for 7(Ground) então retorna um valor TRUE(significando que o play esta no chao)
{
Gizmos.color = Color.green;
Debug.Log(hitInfo.collider);
Debug.Log("TOUCHING GROUND!");
gameObject.transform.parent = null;
isJumping = false;
neutralGravity = 0;
return true;
}
else if (hitInfo.collider.gameObject.layer == 6) // se o raycast colidir com o objeto cujo layer é igual a 6(movingPlatforms) o player vai ser child object do cubo que se mexe
{
Gizmos.color = Color.green;
Debug.Log(hitInfo.collider);
Debug.Log("TOUCHING GROUND!");
gameObject.transform.SetParent(hitInfo.collider.gameObject.transform, true);
isJumping = false;
neutralGravity = 0;
return true;
}
else if (hitInfo.collider == null) // se o raycast nao detetar qualquer collider, o player nao está no chao
{
Gizmos.color = Color.red;
gameObject.transform.parent = null;
isJumping = true;
neutralGravity = gravityDown; // if player is jumping the gravity will pull the player down
return false;
}
else // se o raycast nao detetar qualquer collider, o player nao está no chao
{
gameObject.transform.parent = null;
isJumping = true;
neutralGravity = gravityDown; // if player is jumping the gravity will pull the player down
return false;
}
}
gameObject.transform.parent = null;
isJumping = true;
neutralGravity = gravityDown; // if player is jumping the gravity will pull the player down
return false; // if the player isn't touching the ground he is jumping and not on any platform
}
public bool OnSlope()
{
if (Physics.Raycast(playerCollider.bounds.center, Vector3.down, out slopeHit, slopeCastDistance))
{
Debug.Log(slopeHit.normal);
if (slopeHit.normal != Vector3.up)
{
slopeMoveDirection = Vector3.ProjectOnPlane(playerVector, slopeHit.normal); // creates a vector3 that projects on plane surface from playerVector and hits on surface normal creating a perpendicular vector
// playerRb.AddForce(playerVector + slopeMoveDirection, ForceMode.Force); ; // the velocity will be the playervector + slopemovedirection, which means the vector will move in slope direction
return true;
}
}
return false;
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawRay(playerCollider.bounds.center, Vector3.down * castDistance);
Gizmos.DrawSphere(playerCollider.bounds.center, sphereRadius);
Gizmos.color = Color.yellow;
Gizmos.DrawRay(playerCollider.bounds.center, Vector3.down * slopeCastDistance);
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Debug.Log(hit);
}
}