[SOLVED] - Issues with Character Controller and Moving Platform
I am working on a simple platformer, but with a 3D visualization. I have a capsule character and a moving platform to the right of the character's platform. So this is what it looks like when the scene is enabled:
However, with the character controller I have had several issues with both the Character Controller and the moving platform. First, here is my code for the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private float _speed = 5f;
[SerializeField] private float _gravity = 1.0f;
[SerializeField] private float _jumpHeight = 15.0f;
[SerializeField] private int _coins;
private float _yVelocity;
private bool _canDoubleJump = false;
private CharacterController _controller;
private UIManager _uiManager;
// Variable for player coins
void Start()
{
_controller = GetComponent<CharacterController>();
_uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
if (_uiManager == null)
{
Debug.LogError("The UI Manager is NULL!");
}
}
// Update is called once per frame
void Update()
{
CalculateMovement();
}
private void CalculateMovement()
{
float horizontalInput = Input.GetAxis("Horizontal");
Vector3 direction = new Vector3(horizontalInput, 0, 0);
Vector3 velocity = direction * _speed;
if (_controller.isGrounded)
{
if (Input.GetKeyDown(KeyCode.Space))
{
_yVelocity = _jumpHeight;
_canDoubleJump = true;
}
}
else
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (_canDoubleJump)
{
_yVelocity += _jumpHeight;
_canDoubleJump = false;
}
}
_yVelocity -= _gravity;
}
velocity.y = _yVelocity;
_controller.Move(velocity * Time.deltaTime);
}
public void AddCoins()
{
_coins++;
_uiManager.UpdateCoinDisplay(_coins);
}
}
This is my code for the moving platform:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingPlatform : MonoBehaviour
{
[SerializeField] private Transform _targetA, _targetB;
[SerializeField] private GameObject _player;
private float _speed = 1.0f;
private bool _switching = false;
// Update is called once per frame
void FixedUpdate()
{
if (_switching == false)
{
transform.position = Vector3.MoveTowards(transform.position, _targetB.position, _speed * Time.deltaTime);
}
else if (_switching)
{
transform.position = Vector3.MoveTowards(transform.position, _targetA.position, _speed * Time.deltaTime);
}
if (transform.position == _targetB.position)
{
_switching = true;
}
else if (transform.position == _targetA.position)
{
_switching = false;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
other.transform.parent = transform;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
other.transform.parent = null;
}
}
}
Issue 1: Character Rotation Now, I control my character on the horizontal input. However, when I begin to move left and right, the character begins to spin via the rotation aspect on the transform component. I am unaware of the issue of this, but a temporary solution I found was to put transform.SetPositionAndRotation(transform.position, Quaternion.identity);
in my Update method for my Player script. This works, but I feel this is not the best solution for this problem.
Issue 2: Character changing Scale The scale is set to move between two invisible points, and when the player jumps on top of the platform, the player is set as a child of the platform. However, upon jumping off of the platform, the player looks sometimes like this:
The moving platform has a RigidBody component, in case this may be the cause of this issue
Issue 3: Moving Force Many times when my character jumps on top of my platform, the character is pushed off of the platform on the x axis
I think that the biggest cause of this issue is the setting the character as a parent of the platform. I cannot find a way to only inherit the position, but any help I will greatly appreciate.
Answer by Susorodni · Apr 24, 2020 at 01:33 PM
Okay, I actually found out the issue. I was using a Rigid Body component the whole time, and that messed everything up. I just removed it and everything was resolved.
I've been following this same Udemy course and this solved my problem. Thanks for posting the solution once you found it :)
Just a note for others, the RigidBody was removed from both the $$anonymous$$ovingPlatform and Player.