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

2D Platform Movement [Solved]

Discussion in 'Scripting' started by DomDomDom, Jul 14, 2015.

  1. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Hello guys,

    I'm hoping someone can look over my logic here and give me a hand. I'm trying to make a platform move back and forth. I believe I've set it up correctly so all I have to do is attach it to an object, set the values and let it go.

    This is what I've got so far, and i'm not too sure why its not moving, could be I've stared at my code too long...

    Anyhow;
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MovePlatform : MonoBehaviour {
    6.  
    7.     public bool horizontalPlatform = true;
    8.     public int movementSpeed = 5;
    9.     public int xMin = 5;
    10.     public int xMax = 5;
    11.     public int yMin = 5;
    12.     public int yMax = 5;
    13.  
    14.     private Vector2 startingPos;
    15.     private Vector2 runningPos;
    16.     private float currentPosX;
    17.     private float currentPosY;
    18.     private Vector2 minMaxCheck;
    19.  
    20.     // Use this for initialization
    21.     void Awake () {
    22.    
    23.         if(horizontalPlatform == true){
    24.             HorizontalMovement();
    25.         }else{
    26.             VerticalMovement();
    27.         }
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.  
    33.         if(horizontalPlatform == true){
    34.             runningPos = transform.position;
    35.  
    36.             if(runningPos.x == startingPos.x){
    37.                 transform.Translate(Vector2.right * movementSpeed * Time.deltaTime);
    38.             }else if(runningPos.x >= minMaxCheck.y){
    39.                 transform.Translate(Vector2.left * movementSpeed * Time.deltaTime);
    40.             }else if(runningPos.x <= minMaxCheck.x){
    41.                 transform.Translate(Vector2.right * movementSpeed * Time.deltaTime);
    42.             }
    43.  
    44.         }else{
    45.  
    46.         }
    47.  
    48.     }
    49.  
    50.     void HorizontalMovement(){
    51.  
    52.         startingPos = transform.position;        //Get the current postion
    53.         currentPosX = startingPos.x;            //Get the x value for horizontal movement
    54.         //print(currentPosX + " - currontPosX");
    55.  
    56.         minMaxCheck.x = currentPosX - xMin;     //This is the min X value or Left Check
    57.         minMaxCheck.y = currentPosX + xMax;     //This is the max X vakue or Right Check
    58.  
    59.         //print("Left Check: " + minMaxCheck.x);
    60.         //print("Right Check: " + minMaxCheck.y);
    61.     }
    62.  
    63.     void VerticalMovement(){
    64.  
    65.         startingPos = transform.position;
    66.         currentPosY = startingPos.y;
    67.         //print(currentPosY + " - currontPosY");
    68.     }
    69. }
    70.  
    71.  
    My other thing is, I'm not sure how to move the play in the same direction as the platform with out falling off. Any advice on that would be appreciated.

    Thanks for the help in advanced!
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Because you want something to move back and forth in a loop, this is called a Wave. Use the wave function to make something back and forth based on t time passed. Use Mathf.Sin or Mathf.Cos to get the back and forth working.
     
  3. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Try this, this should get your Transform oscillating.
    Oscillator.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Oscillator : MonoBehaviour {
    5.     [SerializeField] float amplitude = 1.0f;
    6.     [SerializeField] float speed = 1.0f;
    7.     [SerializeField] Axis axis = Axis.XAxis;
    8.  
    9.     Vector3 origin = Vector3.zero;
    10.     float t = 0.0f;
    11.  
    12.     void Start() {
    13.         this.origin = this.transform.position;
    14.     }
    15.  
    16.     void Update() {
    17.         t = Time.realtimeSinceStartup;
    18.  
    19.         Vector3 position = this.transform.position;
    20.  
    21.         switch(this.axis) {
    22.             case Axis.XAxis: {
    23.  
    24.             position.x = this.amplitude * Mathf.Sin(Time.realtimeSinceStartup * this.speed) + this.origin.x;
    25.  
    26.             this.transform.position = position;
    27.  
    28.             } break;
    29.             case Axis.YAxis: {
    30.  
    31.             position.y = this.amplitude * Mathf.Sin(Time.realtimeSinceStartup * this.speed) + this.origin.y;
    32.  
    33.             this.transform.position = position;
    34.  
    35.             } break;
    36.             case Axis.ZAxis: {
    37.             position.z = this.amplitude * Mathf.Sin(Time.realtimeSinceStartup * this.speed) + this.origin.z;
    38.  
    39.             this.transform.position = position;
    40.  
    41.             }break;
    42.         }
    43.     }
    44. }
    45.  
    46. public enum Axis {
    47.     XAxis,
    48.     YAxis,
    49.     ZAxis
    50. }
     
  4. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Thanks mate, appreciate the insight and that script worked out very well!
    Now onto getting the player to stay on the platform.
     
  5. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    HoldObject.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Collider))]
    5. // [RequireComponent(typeof(Collider2D))] // Use this for 2D
    6. public class HoldObject : MonoBehaviour {
    7.     void OnTriggerEnter(Collider coll) {
    8.         coll.transform.parent = this.transform;
    9.     }
    10.  
    11.     void OnTriggerExit(Collider coll) {
    12.         coll.transform.parent = null;
    13.     }
    14.  
    15.     /*
    16.     void OnTriggerEnter2D(Collider2D coll) {
    17.         coll.transform.SetParent(this.transform);
    18.     }
    19.  
    20.     void OnTriggerExit2D(Collider2D coll) {
    21.         coll.transform.SetParent(null);
    22.     }*/
    23. }
    24.  
     
  6. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    HoldObject.cs goes on the platform correct? My player doesn't move with it; I'm using Collider2D.
    Thanks again for sharing this with me. Did you write these or use a reference somewhere?

    Edit) I see OnTriggerEnter2D needs the Ridgedbody attached to it doesn't it? When I do that, and hit my platform is starts spinning and taking parts of my environment with it rather than the player. Do I have this setup wrong? I can post a picture of my inspector if needed.
     
    Last edited: Jul 15, 2015
  7. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    HoldObject.cs goes on the platform. You will need 2 colliders though. Add an extra Collider2D such as a BoxCollider2D and set as a trigger. Adjust the Trigger zone where you want the player to be "held" at. You do not need a rigidbody, if you add a rididbody it will spin if you have the Z Rotation unlocked.
     
  8. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Thanks Polymorphik! I got it working now, though it seems that the player doesn't move evenly with the platform, a bit slower than what the platform moves at. However, when I drop one of my other objects on it, it works perfectly; in this case my medkit object.
     
  9. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    If I may make a suggestion that worked great in my game:

    I made an Parent with two children, one with the mesh, and the other was an offset empty point. I then rotated the empty point so that it made a corkscrew motion. Then I told the mesh to follow the x and y but ignore the z.

    The result was a perfect signwave.
     
  10. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Thanks for the tip mate, I'll try that out too. I still haven't had any luck with the player's position on the platform. It does become the child but it doesn't move at the same speed. I'll keep looking into that one.

    Edit: I fixed the slow movement for the player on the platform. Turns out I had interpolate turn on. Thanks for the help everyone!
     
    Last edited: Jul 18, 2015