when i jump to a moving platform the player gets childed to the cube but it never moves with it
if i remove the character controller it works fine, the thing is that i already tried to change the method to fixedupdate, change the character controller settings on the inspector and nothing works to make it move with the cube. what can i do?
problem: ma3x55
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
float distance = 5f;
public CharacterController playerController;
public Collider playerCollider;
public float verticalMovement;
public Vector3 playerVector;
public float movementSpeed = 15f;
public float jumpForce = 50f;
public float minGravity = -10f;
public bool isJumping = false;
public FixedJoystick joystickMove;
public float x;
public float y;
public float z;
public float gravity = 10f;
// 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()
{
Quaternion cameraRot = Quaternion.Euler(0, gameCamera.eulerAngles.y, 0); // convers camera quaternion to euler, to be able to rotate the camera in euler angles (rotation axis from inspector)
x = joystickMove.Horizontal;
y = verticalMovement;
z = joystickMove.Vertical;
playerVector = cameraRot * new Vector3(x, 0, z) * movementSpeed; // the playervector input will have the x and z values. y is added later
if (isJumping == false && IsGrounded() == true && jumpButtonEvent.jumpBtnClicked == true) // if it meets the 3 requirements, the player will jump
{
verticalMovement = jumpForce; // vertical movement will be equal to the jump force when player jumps
playerVector.y = verticalMovement;
}
//if player is jumping...
// mathf.clamp limits the min value (how far the gravity can push the player) and max value(how far the player will be able to jump)
verticalMovement = Mathf.Clamp(verticalMovement, minGravity, jumpForce) - gravity * Time.deltaTime; // if player is mid-air (jumping) the vertical movement will be subtracted to the gravity, making the gravity pushing the player to the ground
playerVector.y = verticalMovement;
playerController.Move(playerVector * Time.deltaTime);
}
/* public void Jumping()
{
/* if (isJumping == true)
{
verticalMovement = verticalMovement - gravity * Time.deltaTime; // if player is mid-air (jumping) the vertical movement will be subtracted to the gravity, making the gravity pushing the player to the ground
playerVector.y = verticalMovement;
} */
/* 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()
{
Movement();
IsGrounded();
OnSlope();
}
private void FixedUpdate()
{
// Jumping();
// Movement();
}
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;
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;
isJumping = true;
return false;
}
else // se o raycast nao detetar qualquer collider, o player nao está no chao
{
gameObject.transform.parent = null;
isJumping = true;
return false;
}
}
gameObject.transform.parent = null;
isJumping = true;
playerVector.y = verticalMovement; // the y axis of the player vector will be equal to verticalMovement;
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)
{
float slopeAngle = Vector3.Angle(slopeHit.normal, Vector3.up);
Debug.Log(slopeAngle);
// 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);
}
}