If I am running the game in build mode, and when I jump onto the platform, the player moves along with it. But whenever the player dies and goes inactive, resets position and goes active again, it then doesn’t work. Weirdly enough, when I am in the attacking mode, it works, but when I am in the regular moving mode, it doesn’t.
Here are the scripts for my platform:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformMovement : MonoBehaviour
{
public int PlatformDirection = 0;
public int MoveSpeed;
void Update()
{
if (PauseMenu.Paused == 0)
{
if (PlatformDirection == 0)
{
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
}
if (PlatformDirection == 1)
{
transform.Translate(Vector3.back * MoveSpeed * Time.deltaTime);
}
}
}
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "Wall")
{
if (PlatformDirection == 0)
{
PlatformDirection = 1;
}
else
{
PlatformDirection = 0;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformDetector : MonoBehaviour
{
public GameObject Player;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == Player)
{
Player.transform.parent = transform;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject == Player)
{
Player.transform.parent = null;
}
}
}
And the script for my player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float _moveSpeed;
public float _gravity;
public float _jumpSpeed;
public float _bounceSpeed;
public static int movemode;
public GameObject Camera;
public Animator animator;
public AudioSource audioSource;
public AudioClip JumpSound;
public AudioClip BoingSound;
public AudioClip HitSound;
public static int OnIce;
private CharacterController _controller;
private Rigidbody rb;
private float horizontalInput;
private float verticalInput;
public static float _directionY;
// Start is called before the first frame update
void Start()
{
_controller = GetComponent<CharacterController>();
audioSource = gameObject.GetComponent<AudioSource>();
movemode = 0;
PauseMenuController.ExitingLevel = 0;
}
// Update is called once per frame
void Update()
{
if (movemode == 0)
{
_moveSpeed = 5f;
_gravity = 9.81f;
_jumpSpeed = 4f;
if (OnIce == 0)
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
}
if (OnIce == 1)
{
horizontalInput = Input.GetAxis("HorizontalIce");
verticalInput = Input.GetAxis("VerticalIce");
}
Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
direction = Camera.transform.TransformDirection(direction);
if (_controller.isGrounded)
{
if(Input.GetButtonDown("Jump"))
{
audioSource.PlayOneShot(JumpSound, 1);
_directionY = _jumpSpeed;
}
}
if (!_controller.isGrounded)
{
_directionY -= _gravity * Time.deltaTime;
direction.y = _directionY;
}
else
{
_directionY -= 0 * Time.deltaTime;
direction.y = _directionY;
}
_controller.Move(direction * _moveSpeed * Time.deltaTime);
animator.SetBool("isWalking", verticalInput != 0 || horizontalInput != 0);
animator.SetBool("isGrounded", _controller.isGrounded == true);
animator.SetBool("Attacking", movemode == 3);
animator.SetBool("Swimming", movemode == 1);
animator.SetBool("LevelComplete", movemode == 4);
}
if (movemode == 1)
{
_moveSpeed = 3f;
_gravity = 4f;
_jumpSpeed = 1.5f;
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
direction = Camera.transform.TransformDirection(direction);
if(Input.GetButtonDown("Jump"))
{
_directionY = _jumpSpeed;
}
_directionY -= _gravity * Time.deltaTime;
direction.y = _directionY;
_controller.Move(direction * _moveSpeed * Time.deltaTime);
animator.SetBool("Swimming", movemode == 1);
animator.SetBool("Attacking", movemode == 3);
}
if (movemode == 3)
{
animator.SetBool("Attacking", movemode == 3);
}
if (movemode == 4)
{
animator.SetBool("LevelComplete", movemode == 4);
}
}
void OnTriggerEnter (Collider col)
{
if(col.gameObject.name == "Spike")
{
if (Pos_reset.invincibilityframe > 2)
{
HealthCounter.Health -= 1;
if (HealthCounter.Health > 0)
{
audioSource.PlayOneShot(HitSound, 1);
_directionY = _jumpSpeed;
}
Pos_reset.invincibilityframe = 0;
}
}
if(col.gameObject.name == "Trampoline")
{
audioSource.PlayOneShot(BoingSound, 1);
_directionY = _bounceSpeed;
}
}
}
Any ideas of why this is happening?
New contributor
Ooblue365 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.