using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
//NEXT TO DO! Make movement immediately change directions/stop with user commands, not keep going for a bit THEN change
public float walkSpeed = 3.5f;
public bool moving = true;
public bool snapToGrid = false;
public float offset = 0.5f;
public char lastDirection = 'n';
//these 3 used for positioning back onto the grid
float x;
float y;
float z;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetAxis("Vertical") > 0.0) //w
{
if(lastDirection == 'a' || lastDirection == 's' || lastDirection == 'd')
reset();
lastDirection = 'w';
transform.rotation = new Quaternion(0,0,0,0);
if(moving)
transform.Translate(0,0,walkSpeed * Time.deltaTime);
animation.CrossFade ("run");
}
else if(Input.GetAxis("Vertical") < 0.0) //s
{
if(lastDirection == 'w' || lastDirection == 'a' || lastDirection == 'd')
reset();
lastDirection = 's';
transform.rotation = new Quaternion(0,1,0,0);
if(moving)
transform.Translate(0,0,walkSpeed * Time.deltaTime);
animation.CrossFade ("run");
}
else if(Input.GetAxis("Horizontal") > 0.0) //d
{
if(lastDirection == 'w' || lastDirection == 'a' || lastDirection == 's')
reset();
lastDirection = 'd';
transform.rotation = new Quaternion(0,1,0,1);
if(moving)
transform.Translate(0,0,walkSpeed * Time.deltaTime);
animation.CrossFade ("run");
}
else if(Input.GetAxis("Horizontal") < 0.0) //a
{
if(lastDirection == 'w' || lastDirection == 's' || lastDirection == 'd')
reset();
lastDirection = 'a';
transform.rotation = new Quaternion(0,-1,0,1);
if(moving)
transform.Translate(0,0,walkSpeed * Time.deltaTime);
animation.CrossFade ("run");
}
else
{
reset();
}
}
void reset()
{
transform.Translate(0,0,0);
animation.CrossFade ("idle");
//put character back onto the grid -- have to do - as well as + because + doesn't work correctly when the position value is negative
if((int) transform.position.x >= 0)
{
x = transform.position.x - 0.5f; //this way instead of 10.1 -> 10 and 10.6 -> 11, all 10 values will go to 10, because 10.1 -> 9.6 -> 10, 10.6 -> 10.1 -> 10
x = Mathf.RoundToInt(x) + offset;
}
else
{
x = transform.position.x + 0.5f;
x = Mathf.RoundToInt(x) - offset;
}
if((int) transform.position.z >= 0)
{
z = transform.position.z - 0.5f;
z = Mathf.RoundToInt(z) + offset;
}
else
{
z = transform.position.z + 0.5f;
z = Mathf.RoundToInt(z) - offset;
}
y = transform.position.y;
transform.position = new Vector3(x,y,z);
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Wall")
moving = false;
}
void OnCollisionExit(Collision col)
{
if(col.gameObject.tag == "Wall")
moving = true;
}
}
Right now I have a “game” with a top-down view of a box and a character running in it. What I’m TRYING to do is keep the character on a grid, which is why whenever he stops running I round his position ont he x-z plane to n.5 (so 10.2 goes to 10.5, 9.8 goes to 9.5, etc).
However I’m running into a problem: Whenever i try to change directions immediately, rather than go one direction, wait… then go another direction, it won’t immediately change. Instead it keeps running for a little bit, THEN changes. So how can I stop the movement in mid-movement? I tried to “overwrite” it with the (0,0,0) line at the top of Reset, but that didn’t help
Thanks for any/all help!