Search Unity

Question Moving with platform

Discussion in 'Visual Scripting' started by robby_develop, Oct 15, 2023.

  1. robby_develop

    robby_develop

    Joined:
    Oct 15, 2023
    Posts:
    1
    I'm making a 3D game where there are moving platforms, and I want the player to stay there while they move, so I tried this script

    Code (CSharp):
    1. public class MoveWithFloor : MonoBehaviour
    2. {
    3.     CharacterController player;
    4.  
    5.     Vector3 groundPosition;
    6.     Vector3 lastGroundPosition;
    7.     string groundName;
    8.     string lastGroundName;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         player = this.GetComponent<CharacterController>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (player.isGrounded)
    20.         {
    21.             RaycastHit hit;
    22.  
    23.             if (Physics.SphereCast(transform.position, player.height / 2.8f, -transform.up, out hit))
    24.             {
    25.                 GameObject groundedIn = hit.collider.gameObject;
    26.                 groundName = groundedIn.name;
    27.                 groundPosition = groundedIn.transform.position;
    28.  
    29.                 if (groundPosition != lastGroundPosition && groundName == lastGroundName)
    30.                 {
    31.                     this.transform.position += groundPosition - lastGroundPosition;
    32.                     player.enabled = false;
    33.                     player.transform.position = this.transform.position;
    34.                     player.enabled = true;
    35.                 }
    36.  
    37.                 lastGroundName = groundName;
    38.                 lastGroundPosition = groundPosition;
    39.             }
    40.         }
    41.         else if (!player.isGrounded)
    42.         {
    43.             lastGroundName = null;
    44.             lastGroundPosition = Vector3.zero;
    45.         }
    46.     }
    47.  
    48.     private void OnDrawGizmos()
    49.     {
    50.         player = this.GetComponent<CharacterController>();
    51.         Gizmos.DrawWireSphere(transform.position, player.height / 2.8f);
    52.     }
    53.  
    54.     private void Awake()
    55.     {
    56.         Application.targetFrameRate = 60;
    57.     }
    58. }
    and it kinda works, but the player and platform shake when they are in contact, so I tried this other code inside the platforms' movement script

    Code (CSharp):
    1. private void OnCollisionEnter(Collision collision)
    2. {
    3.     if (collision.gameObject.CompareTag("Player"))
    4.     {
    5.         collision.gameObject.transform.SetParent(transform);
    6.     }
    7. }
    8.  
    9. private void OnCollisionExit(Collision collision)
    10. {
    11.     if (collision.gameObject.CompareTag("Player"))
    12.     {
    13.         collision.gameObject.transform.SetParent(null);
    14.     }
    15. }
    But it just doesn't work, the player has the tag and I don't know what to do, also I would like to know how to make it so the code only activates when the player is hitting the platform from above