I need a script in C# which moves an object in only one direction at a time.
I was trying to program a simple 2D game with C# and have run into a dilemma of sorts. I am quite inexperienced with C#, and this is my first time using it. The game is meant to be played from a top-down perspective, but it has a very particular movement system. You can only move vertically and horizontally, so diagonal movement should be disabled. when you do press one of the arrow keys to move, the player character only moves up one step, so holding the button down so the character keeps walking should not be possible.
This is what I’ve managed to get so far:
using UnityEngine;
using System.Collections;
public class Control : MonoBehaviour {
public float speed;
void Update()
{
Vector2 movement = new Vector2 ();
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
if(Input.GetButton("Vertical"))
{
transform.Translate(movement(1,speed,1) * Time.deltaTime);
return;
}
if(Input.GetButton("Horizontal"))
{
transform.Translate(movement(speed,1,1) * Time.deltaTime);
return;
}
if(Input.GetButton("-Vertical"))
{
transform.Translate(movement(1,-speed,1) * Time.deltaTime);
return;
}
This script is supposed to only allow horizontal and vertical movement, however I am getting the error “Movement is a variable but is used like a method”.
Sorry for the long question and thanks in advance.