anyone knows why my player movement wont slide on slope surfaces, if i disable player movement script it will slide normally
bsy3xr
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour
{
public Collider playerCollider;
public Rigidbody playerRb;
// private Vector3 movement;
public Vector3 playerVector;
public float movementSpeed = 15f;
public float defaultSpeed = 15f;
public float jumpForce = 500f;
public bool isJumping = false;
public FixedJoystick joystickMove;
public Button jumpButton;
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;
// Start is called before the first frame update
void Start()
{
raycastDirection = Vector3.down;
playerRb = GetComponent<Rigidbody>();
playerCollider = GetComponent<SphereCollider>();
}
public void Movement()
{
float x = joystickMove.Horizontal;
float y = playerRb.velocity.y;
float z = joystickMove.Vertical;
playerVector = gameCamera.transform.rotation * new Vector3(x, 0, z); // the playervector input will have the x and z values. y is added later
playerVector = Vector3.ProjectOnPlane(playerVector, Vector3.up); // projects a vector from the playerVector origin that is perpendicular to the surface normal
playerVector.Normalize(); // a vector of only 1 length magnitude
playerVector = playerVector * movementSpeed; // multiplies the playervector with the movementspeed variable;
playerVector.y = y; // add a value to the Y axis from playerVector
playerRb.velocity = playerVector;
}
// Update is called once per frame
void Update()
{
Debug.Log(OnSlope());
Debug.Log(isJumping);
// Debug.Log(OnSlope());
}
private void FixedUpdate()
{
Movement();
MovementButtonRelease();
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)
{
playerRb.velocity = new Vector3(0, playerRb.velocity.y, 0); // playerRB.velocity.y (evita que haja alteracoes á fisica do jogo no eixo Y (de cima pra baixo))
}
}
public void Jumping()
{
if (isJumping == false && IsGrounded() == true)
{
playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isJumping = true;
Debug.Log("jump");
}
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
}
}
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;
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;
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;
return false;
}
else // se o raycast nao detetar qualquer collider, o player nao está no chao
{
gameObject.transform.parent = null;
return false;
}
}
gameObject.transform.parent = null;
isJumping = true;
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.velocity = slopeMoveDirection;
}
}
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);
}
}