Parent the player to a moving platform in fixedUpdate
I have a player with a CharacterController. The script that controlls the inputs is called "PlayerController" and the one that controlls the platform "MovingPlatform". When the player collides with the moving platform's parent, that works perfect.
The problem comes when I change the Update method of the "PlayerContoller" for one FixedUpdate the player parents to the platform but doesn't stick to the platform.
This is the PlayerContoller script:
private void FixedUpdate() {
isGrounded = controller.isGrounded;
InputX = Input.GetAxis("Horizontal");
InputZ = Input.GetAxis("Vertical");
Speed = new Vector2(InputX, InputZ).sqrMagnitude;
var camera = Camera.main;
var forward = cam.transform.forward;
var right = cam.transform.right;
forward.y = 0f;
right.y = 0f;
forward.Normalize();
right.Normalize();
desiredMoveDirection = forward * InputZ + right * InputX;
if (!forceField.activeInHierarchy)
{
shoulderMovement.enabled = true;
shotcon.enabled = true;
}
if (forceField.activeInHierarchy)
{
shoulderMovement.enabled = false;
shotcon.enabled = false;
}
if (groundCheck == true)
{
anim.SetBool("isGrounded", true);
}
if (groundCheck == false)
{
anim.SetBool("isGrounded", false);
}
onSlope = OnSlope();
if (isGrounded)
{
velocity = 0.0f;
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
moveDirection = desiredMoveDirection * velocity;
particula1.SetActive(true);
particula2.SetActive(true);
particulaJump.SetActive(true);
isJumping = false;
if (Input.GetButtonDown("Jump") && !forceField.activeInHierarchy)
{
MakeJump();
}
}
else
{
if (!onSlope)
{
velocity = 3f;
moveDirection.x = desiredMoveDirection.x * velocity;
moveDirection.z = desiredMoveDirection.z * velocity;
}else if (onSlope)
{
isJumping = false;
if (!isJumping && Input.GetButtonDown("Jump") && !forceField.activeInHierarchy)
{
MakeJump();
}
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
This is the MovingPlatform script:
public class MovingPlatform : MonoBehaviour {
public Transform [] waypoints;
private int current = 0;
private Transform[] inverse;
public float speed;
private float WP_radius = .1f;
public GameObject[] bones;
protected void FixedUpdate()
{
if(Vector3.Distance(waypoints[current].transform.position, transform.position) < WP_radius)
{
current++;
if(current >= waypoints.Length)
{
current = 0;
}
}
transform.position = Vector3.MoveTowards(transform.position, waypoints[current].transform.position, Time.deltaTime * speed);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
other.GetComponent<CharacterController>().Move(Vector3.down *2);
other.transform.parent = transform;
//desactivar dynamic bone
foreach (GameObject bone in bones)
{
bone.GetComponent<DynamicBone>().enabled = false;
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
other.transform.parent = null;
//activar dynamic bone
foreach (GameObject bone in bones)
{
bone.GetComponent<DynamicBone>().enabled = true;
}
}
}
}
Comment