Hey,
I’m fairly new to both unity and c#. I’m trying to program top-down grid based movement similar to the Pokemon games, moving from one tile to the next. I have a movement script that works, but not in the way I want it to. I found a JavaScript version earlier that does what I want but I can’t figure out how to translate it to c# as I have no experience with JavaScript whatsoever.
Here is my current movement script in c# :
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour
{
public float speed = 2.5f;
bool canMove;
void Start ()
{
bool canMove = true;
}
void Movement ()
{
if (Input.GetKey (KeyCode.W))
{
transform.Translate(Vector3.up * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.A))
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.S))
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.D))
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
}
void FixedUpdate ()
{
if (canMove = true)
{
Movement ();
}
}
}
Here is the example I found in JavaScript:
#pragma strict
private var speed = 2.0;
private var pos : Vector3;
private var tr : Transform;
function Start() {
pos = transform.position;
tr = transform;
}
function Update()
{
if (Input.GetKey(KeyCode.D) && tr.position == pos)
{
pos += Vector3.right;
}
else if (Input.GetKey(KeyCode.A) && tr.position == pos)
{
pos += Vector3.left;
}
else if (Input.GetKey(KeyCode.W) && tr.position == pos)
{
pos += Vector3.up;
}
else if (Input.GetKey(KeyCode.S) && tr.position == pos)
{
pos += Vector3.down;
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
}
I would greatly appreciate any help anybody could give me with this problem. Thanks