Hi, I’m making a 2d game with no character controls and just a camera. A map (plane with a texture) takes up the view of the whole camera but it’s larger than the camera can see. Basically I’m wondering if there is a way to write a script that allows you to change an objects position using w,s,a,d. Something like when I push ‘w’ 1 is added to the maps y position. I’ve looked into this but can’t seem to find anything about altering just one value at a time. Any help would be greatly appreciated ![]()
There are multiple different ways of both getting input and moving things around. Spending some time with basic tutorials will give you the perspective to know what approach to take with your specific game. Here is one script to get you started:
#pragma strict
public var speed = 1.5;
public var spacing = 1.0;
private var pos : Vector3;
function Start() {
pos = transform.position;
}
function Update() {
if (Input.GetKeyDown(KeyCode.W))
pos.y += spacing;
if (Input.GetKeyDown(KeyCode.S))
pos.y -= spacing;
if (Input.GetKeyDown(KeyCode.A))
pos.x -= spacing;
if (Input.GetKeyDown(KeyCode.D))
pos.x += spacing;
transform.position = Vector3.MoveTowards(transform.position, pos, speed * Time.deltaTime);
}