- Home /
CharacterController and moving platform issues
I have what I believe is a standard FPS controller where I call HandleMovement() in every update (not FixedUpdate). The actual movement happens with characterController.Move(moveDirection * Time.deltaTime);. If I comment this out, my character moves with a platform. Of course this also means I literally cannot move or even rotate the camera. With that line of code not commented out, the player doesn't move with the platform at all.
Here's the full code in case it's relevant:
void HandleMovement() {
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
bool isRunning = Input.GetKey(KeyCode.LeftShift);
float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove && characterController.isGrounded) {
moveDirection.y = jumpSpeed;
} else {
moveDirection.y = movementDirectionY;
}
if (!characterController.isGrounded) {
moveDirection.y -= gravity * Time.deltaTime;
}
// characterController.Move(moveDirection * Time.deltaTime);
if (canMove) {
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
The platform moves very simply and makes the player a child object like so:
private void FixedUpdate() {
if (active) {
Vector3 newPos = transform.position + move;
rb.MovePosition(newPos);
}
}
private void OnTriggerEnter(Collider other) {
if (other.tag == "Player") {
other.transform.SetParent(transform);
active = true;
}
}
I feel like I must be missing something pretty obvious as to why this is an issue. I'm on Unity 2020.1.16f1.
Answer by smugleafdev · Jan 08, 2021 at 01:39 AM
I guess the answer is to just use transform.Translate() in Update. Every tutorial I read said to use physics and .MovePosition, but .Translate seems to be working fine.
Your answer
Follow this Question
Related Questions
How can i update an objects direction of gravity based on its local rotation? 0 Answers
making "one-way" platform 1 Answer
Moving Platform and CharacterController.Move not working. The player falls 3 Answers
What is this error and how can i fix it? 1 Answer
How do you make a characterController jump automatically when it reaches the edge of a platform? 0 Answers