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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Moving platform script

Discussion in '2D' started by Phenomenal_Gamer, Oct 22, 2018.

  1. Phenomenal_Gamer

    Phenomenal_Gamer

    Joined:
    Oct 15, 2018
    Posts:
    10
    Hi, I am a begginer in unity and am planning to make a 2D game. Can anyone please provide me a script for moving a platform continuously from the start of game(through x axis). It would be of great help!!
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    with transform
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Platform : MonoBehaviour {
    6.  
    7.     public float speed;
    8.  
    9.     void Update ()
    10.     {
    11.         transform.Translate(Vector2.right * speed * Time.deltaTime);
    12.     }
    13. }
    with physics
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Platform : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     Rigidbody2D rb;
    9.  
    10.     void Start ()
    11.     {
    12.         rb = GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.     void FixedUpdate ()
    16.     {
    17.         rb.velocity = Vector2.right * speed;
    18.     }
    19. }
     
  3. KRice

    KRice

    Joined:
    Jun 12, 2018
    Posts:
    7
    Hi there, I hope this is useful to you!

    This is a script I created to control the movement of my camera from one point to another. This script uses the Vector3.MoveTowards() function. All you need to do is attach this script to whatever object you want to move (sprite, camera, anything really) and then place an empty object into your scene at the location you wish for the object to stop. Then, drag the empty object in your scene into the "Target" area for the script in the inspector (see image below).

    upload_2018-10-22_16-53-53.png


    Code (CSharp):
    1.     public Transform target; // the target position
    2.     public float speed; // speed - units per second (gives you control of how fast the object will move in the inspector)
    3.     public bool moveObj; // a public bool that allows you to toggle this script on and off in the inspector
    4.    
    5.     // Update is called once per frame
    6.     void Update () {
    7.         if(moveObj == true)
    8.         {
    9.             float step = speed * Time.deltaTime; // step size = speed * frame time
    10.             transform.position = Vector3.MoveTowards(transform.position, target.position, step); // moves position a step closer to the target position
    11.         }      
    12.     }
     
    timoelgamml and Phenomenal_Gamer like this.
  4. Phenomenal_Gamer

    Phenomenal_Gamer

    Joined:
    Oct 15, 2018
    Posts:
    10
    Thank you so much!!!! It really helped
     
    Voidazord and alissonvegea like this.
  5. Phenomenal_Gamer

    Phenomenal_Gamer

    Joined:
    Oct 15, 2018
    Posts:
    10
    Oh...this is more than what I was looking for...thank you soo much!!!
     
    KRice likes this.