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

Moving platform script not behaving how i expected. C#

Discussion in 'Scripting' started by pixelfixation, Jun 23, 2014.

  1. pixelfixation

    pixelfixation

    Joined:
    Feb 7, 2014
    Posts:
    29
    The issue i'm having is the interaction between my character controller with animated platforms. i posted a thread 2 days ago about it and had a couple good suggestions. the issue is that when i stand on my moving platforms the physics don't apply to my character and it just slides off.

    One suggestion I received was to pass my character controller a direction and speed variable then translate the player along a parallel vector to the platform. I tried that approach but it didn't have the effect I expected it too.

    I have 2 objects and scripts involved with this problem, a section of my Player controller script and my moving platforms script.

    The Player Controller Script
    Code (CSharp):
    1. // physics fix for moving platforms
    2.     void OnCollisionStay2D(Collision2D other)
    3.     {
    4.         if(other.transform.tag == "Moving")
    5.         {
    6.             Vector3 direction = other.transform.position;
    7.             speed = other.gameObject.GetComponent<ComplexPlatform>().moveSpeed;
    8.             transform.position = Vector3.MoveTowards (transform.position, direction, 0.15f);
    9.         }
    10.  
    11.     }
    and

    The moving platform script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ComplexAndOverMyHeadPlatform: MonoBehaviour
    5. {
    6.     public Transform[] targets;
    7.     public float moveSpeed;
    8.     private int currentTarget;
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start ()
    13.     {
    14.         transform.position = targets [0].position;
    15.         currentTarget = 0;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update ()
    20.     {
    21.      
    22.         if (transform.position == targets [currentTarget].position)
    23.         {
    24.             currentTarget++;
    25.         }
    26.      
    27.         if (currentTarget >= targets.Length)
    28.         {
    29.             currentTarget = 0;
    30.         }
    31.      
    32.         transform.position = Vector3.MoveTowards (transform.position, targets [currentTarget].position, moveSpeed * Time.deltaTime);
    33.     }
    34. }
    35.  
    The result is my player rapidly bounces vertically and is dragged to the middle of the platform when on the platform.

    I think i have a logic error and i am maybe moving toward the platforms transform but i'm not sure. i would really appreciate the help and if you could try to be specific with your answer. I'm really new to programming and knowing the names of the functions i need really really helps me.
     
  2. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    I'm not sure what's your problem here. Is it that your player is not moving together with moving platform?
    Then why don't you make him a child of this platform upon landing and detach from the platform upon jumping?
     
  3. pixelfixation

    pixelfixation

    Joined:
    Feb 7, 2014
    Posts:
    29
    I tried that first and had some really trippy results (maybe i did it wrong?). the issue i'm having is that i get pulled to the center of the platform instead of traveling in a parallel line with it.
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Making the player a child of the platform should work, can you post an example of your "trippy" results?