smoothly going forward to the center of one tile at a time
im very close to this but still haven't succeeded, what i want is my player to move forward continuously over a plane that is divided into tiles, the player should move one tile at a time smoothly so
endPoint = new Vector3(transform.position.x,transform.position.y,transform.position.z+1);
doesnt quite work (although it seems the best so far) as it steps, stops,steps,stops and i just want it to smoothly glide over each tile, but the player should only be able to rotate 90' left or right, or jump, when it is in the middle of a tile, so after some advice from someone with more experience than myself i have set up my tiles (custom plane 1x1) and set an empty gameobject on each with a OnCollisionStay listener that says if player is on a tile player can rotate or jump, this doesn't quite work as the player is not set to the tiles center, so often the player ends up on the edge of the tiles instead of staying in the center. i essentially want a big grid that the player cannot deviate from so no diagonals, what ive done so far is this
using UnityEngine; using System.Collections;
public class experimentalCharacter : MonoBehaviour {
public float rotateSpeed = 0.1f;
public float speed = 0.1f;
public float jumpSpeed = 6.0f;
public float maxDistance = 1.0f;
float distToGround;
float journeyDistance;
float startTime;
bool isGrounded = false;
// bool doMovement = false;
Vector3 startPoint;
Vector3 endPoint;
Rigidbody rb;
Transform sonicDroid;
void Awake()
{
sonicDroid = transform;
rb = GetComponent<Rigidbody>();
}
void Start()
{
startPoint = sonicDroid.position;
//this is where i tried changing endpoint to step once using this
//endPoint = new Vector3(startPoint.x, startPoint.y, startPoint.z + 1);
endPoint = startPoint;
startTime = Time.time;
rb.AddForce(1, 1, 1);
journeyDistance = Vector3.Distance(startPoint, endPoint);
}
void Update()
{
if (isGrounded)
{
if (Input.GetKeyDown("space"))
{
// doMovement = false;
isGrounded = false;
rb.velocity = Vector3.up * jumpSpeed;
}
else
{
float distanceMoved = (Time.time - startTime) * speed;
float journeyFraction = distanceMoved / journeyDistance;
sonicDroid.position = Vector3.Lerp(startPoint, endPoint, journeyFraction);
float distance = Vector3.Distance(sonicDroid.position, endPoint);
if (distance < maxDistance)
{
Debug.Log("distance:" + distance);
startPoint = sonicDroid.position;
endPoint += (sonicDroid.forward * speed);
}
else
{
Debug.Log("distance:" + distance);
}
}
}
}
void OnCollisionStay(Collision collision)
{
if (!isGrounded && collision.collider.tag.ToLower() == "floor")
{
isGrounded = true;
}
//doMovement = isGrounded;
if (isGrounded && collision.collider.tag.ToLower() == "target")
{
startPoint = sonicDroid.position;
endPoint = startPoint + (sonicDroid.forward * speed);
if (Input.GetKeyDown("a"))
{
sonicDroid.Rotate(new Vector3(0, 90, 0));
Debug.Log("you turned right");
}else if (Input.GetKeyDown("d"))
{
sonicDroid.Rotate(new Vector3(0, -90, 0));
Debug.Log("you turned left");
}
}
}
}
any and all suggestions welcome