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!!
with transform
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Platform : MonoBehaviour {
public float speed;
void Update ()
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
}
with physics
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Platform : MonoBehaviour {
public float speed;
Rigidbody2D rb;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate ()
{
rb.velocity = Vector2.right * speed;
}
}
2 Likes
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).
public Transform target; // the target position
public float speed; // speed - units per second (gives you control of how fast the object will move in the inspector)
public bool moveObj; // a public bool that allows you to toggle this script on and off in the inspector
// Update is called once per frame
void Update () {
if(moveObj == true)
{
float step = speed * Time.deltaTime; // step size = speed * frame time
transform.position = Vector3.MoveTowards(transform.position, target.position, step); // moves position a step closer to the target position
}
}
3 Likes
Thank you so much!!! It really helped
2 Likes
Oh…this is more than what I was looking for…thank you soo much!!!
1 Like