- Home /
Question by
BitGlitch · Oct 05, 2014 at 09:39 AM ·
movementsmoothtranslatetranslation
Smoothing out translation for movement?
Alright, so I have been working with my code for hours in different ways to try and make the translation happen over time, but I can't get it right. Anyone out there know what needs to be done?
using UnityEngine;
using System.Collections;
public class MoveScript : MonoBehaviour {
public bool forwardObstruction = false; // Next few bools are to register if there is an obstruction-
public bool backwardObstruction = false; // -in the given direction.
public bool leftObstruction = false;
public bool rightObstruction = false;
public float inputDelay = 0.0125f; //Stating how many seconds before we can move again.
public float gridSize = 2f;
public float lineLength = 2f;
private float timestamp;
// Update is called once per frame
void Update () {
//Creates a raycast line forward based on lineLength, checks to see if path is obstructed.
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, lineLength)){
forwardObstruction = true;
}
else
forwardObstruction = false;
Vector3 bwd = transform.TransformDirection(Vector3.back);
if (Physics.Raycast (transform.position, bwd, lineLength)) {
backwardObstruction = true;
}
else
backwardObstruction = false;
Vector3 left = transform.TransformDirection(Vector3.left);
if (Physics.Raycast (transform.position, left, lineLength)) {
leftObstruction = true;
}
else
leftObstruction = false;
Vector3 right = transform.TransformDirection(Vector3.right);
if (Physics.Raycast (transform.position, right, lineLength)) {
rightObstruction = true;
}
else
rightObstruction = false;
//See if the "Forward" key is pressed
if (!forwardObstruction && Time.time >= timestamp && Input.GetButtonDown ("Forward")) {
transform.Translate(Vector3.forward * gridSize, Space.Self);
timestamp = Time.time + inputDelay;
forwardObstruction = forwardObstruction;
}
//See if the "Backward" key is pressed
if (!backwardObstruction && Time.time >= timestamp && Input.GetButtonDown ("Backward")) {
transform.Translate(Vector3.back * gridSize, Space.Self);
timestamp = Time.time + inputDelay;
backwardObstruction = backwardObstruction;
}
//See if the "Left" key is pressed
if (!leftObstruction && Time.time >= timestamp && Input.GetButtonDown ("Left")) {
transform.Translate(Vector3.left * gridSize, Space.Self);
timestamp = Time.time + inputDelay;
leftObstruction = leftObstruction;
}
//See if the "Right" key is pressed
if (!rightObstruction && Time.time >= timestamp && Input.GetButtonDown ("Right")) {
transform.Translate(Vector3.right * gridSize, Space.Self);
timestamp = Time.time + inputDelay;
rightObstruction = rightObstruction;
}
}
}
Thanks for any help received!
Comment