[Solved]Looking for platform movement script

I am looking for a script to move a platform left and right, left when A or the left arrow key is pushed and right when D or the right arrow key is pushed, think DX Ball movement :slight_smile: the game is going to be played purely on a 2D perspective.

If any more information is needed then please let me know :slight_smile:

You can do this with code like the following:-

var speed: float;

function Update() {
    var dir = Input.GetAxis("Horizontal");
    platform.transform.Translate(Vector3.right * dir * speed * Time.deltaTime);
}

Here, the speed variable is set from the inspector to whatever speed works well for the game.

You could also look what is being done in the 2d tutorial that you can find here

I’t another way of moving the plateform…

Thank you very much for the script but i seem to be having one problem, the console is saying (Unknown identifier: ‘platform’) i thought it might be to do with what my platform was called so i changed the name to platform and tagged it as platform as well but the error is still there

Your error means that you are missing something that is known as “platform”.

So you will have to go at it step by step and see what you have missed or forgotten.

I made an entirely new project, created a cube, named it ‘platform’ and tagged it ‘platform’, then attached the script and still getting the same error.

The name of a script variable is completely separate from the names of objects in the scene, so giving a variable the same name as an object doesn’t make it access that object. You can find an object from its name using the GameObject.Find function:-

var platform = GameObject.Find("Platform");

Thanks for the reply, i know bugger all about scripting, i have watched some tutorials in the past but still have very little understanding, i’ve got the script laid out this way which works but still gives me an error saying ‘UnityException: You are not allowed to call this function when declaring a variable.’

var speed: float; 
var platform = GameObject.Find("Platform");


function Update() { 
    var dir = Input.GetAxis("Horizontal"); 
    platform.transform.Translate(Vector3.right * dir * speed * Time.deltaTime); 
	}

try this

var Speed1 = 20;
var Speed2 = 15;
var Rota = 50;
function Update () 
{
	if (Input.GetKey("w"))
	{
		transform.Translate(0, 0, Speed1*Time.deltaTime);
	}

	if (Input.GetKey("s"))
	{
		transform.Translate(0, 0, -Speed1*Time.deltaTime);
	}
	
	if (Input.GetKey("a"))
	{
		transform.Translate(Speed2*Time.deltaTime, 0, 0);
	}
	
	if (Input.GetKey("d"))
	{
		transform.Translate( -Speed2*Time.deltaTime, 0, 0);
	}
	
	if (Input.GetKey("up"))
	{
		transform.Translate(0,Speed2*Time.deltaTime, 0);
	}
	
	if (Input.GetKey("down"))
	{
		transform.Translate(0, -Speed2*Time.deltaTime, 0);
	}
	
	if (Input.GetKey("left"))
	{
		transform.Rotate(0, -Rota*Time.deltaTime, 0); 
	}
	
	if (Input.GetKey("right"))
	{
		transform.Rotate(0, Rota*Time.deltaTime, 0);
	}
}

(up down left right) (wasd) keys control (up down turn left turn right) (foreward left back right) respectively just attatch it to any object you want to controll :slight_smile:

there are visible sliders to controll speed of different axis’s in the editor too

You can call GameObject.Find from the Start function:-

var platform: GameObject;
   ...

function Start() {
   platform = GameObject.Find("Platform");
}

Thanks for the help all, That Homeless Guy your script worked a treat and i’ve managed to edit a couple of bits myself to make it work perfectly :slight_smile:

I’m actually compiling a script that allows much more control over the script down to each rotation axis and movement axis with check boxes for conditionals such as world or local movement as well as constannt push factors for each axis of movement and rotation including a sprint function :slight_smile: i’ll publish it here once it’s completed its designed for aplying to multiple player objects to allow full controll of each player object from the editor pane.

That would be awesome and i’m sure many more would appreciate your effort :slight_smile: