I really can’t figure this out.I am very new to coding andI am trying to make it so an object moves when you press the arrow keys.This is the code I came up with (It is C#):
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
enum selectedId : int{_}
enum speed { _ ,
function ,Update}
.GetKey KeyCode.UpArrow) transform.Translate (Vector3(0,1,0) * Time.deltaTime*speed;
if (Input.GetKey (KeyCode.DownArrow)) transform.Translate (Vector3(0,-1,0) * Time.deltaTime*speed);
if (Input.GetKey (KeyCode.LeftArrow)) transform.Translate (Vector3(-1,0,0) * Time.deltaTime*speed);
if (Input.GetKey (KeyCode.RightArrow)) transform.Translate (Vector3(1,0,0) * Time.deltaTime*speed);
}
function OnMouseDown () {
selectedId = GetInstanceID();
}
The original code posted is a mix of some unityscript crap and an empty c# class, the methods executed are also formatted incorrectly as syntax plays a huge role in catching coding errors. You might want to check out the learning videos unity has created. With that said.
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
public float speed = 2.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.UpArrow))
transform.Translate (new Vector3(0,1,0) * Time.deltaTime*speed);
if (Input.GetKey (KeyCode.DownArrow))
transform.Translate (new Vector3(0,-1,0) * Time.deltaTime*speed);
if (Input.GetKey (KeyCode.LeftArrow))
transform.Translate (new Vector3(-1,0,0) * Time.deltaTime*speed);
if (Input.GetKey (KeyCode.RightArrow))
transform.Translate (new Vector3(1,0,0) * Time.deltaTime*speed);
}
}
You will have to attach this script to a gameobject you want to move. You will also want to manipulate the speed variable as i set it with a default of 2.0f, but will be available in the inspector to dork around with.