Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

The player doesn't move with the platform

Discussion in 'Scripting' started by safak93, Feb 23, 2014.

  1. safak93

    safak93

    Joined:
    Feb 23, 2014
    Posts:
    19
    Hey guys.

    I'm currently working on my first 2D Platformer Game(I'm a newbie :D).
    I have a moving platform, but with 2 problems. First of all when the player stays on a horizontal moving platform he doesn't move with the platform. The second problem is, when the platform move up and down, the player stays not correctly on the platform.

    How can I fix that? I hope you can help me.

    Here is a video, that explains it better:


    PlayerControllerScript:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControllerScript : MonoBehaviour
    5. {
    6.     public float maxSpeed = 6.5f; //How fast the character can be go at the maximum
    7.     bool facingRight = true;
    8.    
    9.     Animator anim;
    10.    
    11.     bool grounded = false;          //fall
    12.     public Transform groundCheck;
    13.     float groundRadius = 0.2f;
    14.     public LayerMask whatIsGround;  //What is ground exactly
    15.     public float jumpForce = 950f;
    16.    
    17.     // Use this for initialization
    18.     void Start ()
    19.     {
    20.         anim = GetComponent<Animator>();
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void FixedUpdate ()
    25.     {
    26.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround); //Here we are in the ground
    27.         anim.SetBool("Ground", grounded); //I'm on ground or not
    28.        
    29.         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y); //Current Vertical Speed
    30.        
    31.        
    32.         if (!grounded)return;
    33.        
    34.         float move = Input.GetAxis ("Horizontal");
    35.        
    36.         anim.SetFloat ("Speed", Mathf.Abs (move));
    37.        
    38.         rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
    39.        
    40.         if(move > 0 !facingRight)
    41.             Flip ();
    42.         else if (move < 0  facingRight)
    43.             Flip ();
    44.     }
    45.    
    46.     void Update()
    47.     {
    48.         if(grounded  Input.GetKeyDown(KeyCode.Space)) //For Jumping you must click the Space-Button
    49.         {
    50.             anim.SetBool("Ground", false); //If we are not in the ground
    51.             rigidbody2D.AddForce(new Vector2(0, jumpForce)); //We are jumping now
    52.         }
    53.     }
    54.    
    55.     void Flip()
    56.     {
    57.         facingRight = !facingRight;
    58.         Vector3 theScale = transform.localScale;
    59.         theScale.x *= -1;
    60.         transform.localScale = theScale;
    61.     }
    62. }
    63.  
    PlatformMover Script:
    Code (csharp):
    1. #pragma strict
    2. private var Xpos : float;
    3. private var Ypos : float;
    4. private var max : boolean;
    5.  
    6. var Vert : boolean;
    7. var maxAmount : int;
    8. var step : float;
    9.  
    10. function Start () {
    11.     Xpos = transform.position.x;
    12.     Ypos = transform.position.y;
    13. }
    14.  
    15. function Update () {
    16.     //SET THE MAX
    17.     if(Vert) {
    18.         if(transform.position.y >= Ypos + maxAmount) {
    19.         max = true;
    20.         } else if(transform.position.y <= Ypos){
    21.             max = false;
    22.         }
    23.  
    24.     } else {    //Horizontal
    25.         if(transform.position.x >= Xpos + maxAmount){
    26.         max = true;
    27.             } else if(transform.position.x <= Xpos){
    28.             max = false;
    29.         }
    30.     }
    31.  
    32.     //MOVING THE PLATFORM
    33.     if(Vert){   // Vertical movement
    34.         if(!max){
    35.             transform.position.y += step;
    36.         } else {
    37.             transform.position.y -= step;
    38.         }
    39.     } else {    //Horizontal movement
    40.         if(!max){
    41.             transform.position.x += step;
    42.         } else {
    43.             transform.position.x -= step;
    44.         }
    45.     }
    46. }
    47.  
    Here is the inspector from the green block:
     
    Last edited: Feb 23, 2014
  2. safak93

    safak93

    Joined:
    Feb 23, 2014
    Posts:
    19
    Can anyone help me?
     
  3. Bloody-Swamp

    Bloody-Swamp

    Joined:
    Jul 30, 2012
    Posts:
    37
    I solved this problem before by making my player child of the movable platform and release him when the collision went off.
    I'm sure there are better solutions out there though :p