public class PlayerMovement : MonoBehaviour
{
private float m_distanceTraveled = 0f;
private float distance = 74f;
private void Start()
{
}
void Update()
{
Move();
}
private void Move()
{
if (Input.GetKeyDown(KeyCode.W))
{
if (m_distanceTraveled < distance)
{
Vector3 oldPosition = transform.position;
transform.Translate(0, 0, distance);
m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
m_distanceTraveled = 0f;
}
}
if (Input.GetKeyDown(KeyCode.S))
{
if (m_distanceTraveled < distance)
{
Vector3 oldPosition = transform.position;
transform.Translate(0, 0, -distance);
m_distanceTraveled = Vector3.Distance(oldPosition, transform.position);
m_distanceTraveled = 0f;
}
}
if (Input.GetKeyDown(KeyCode.D))
{
if (m_distanceTraveled < distance)
{
Vector3 oldPosition = transform.position;
transform.Translate(distance, 0, 0);
m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
m_distanceTraveled = 0f;
}
}
if (Input.GetKeyDown(KeyCode.A))
{
if (m_distanceTraveled < distance)
{
Vector3 oldPosition = transform.position;
transform.Translate(-distance, 0, 0);
m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
m_distanceTraveled = 0f;
}
}
}
}
This is my code.
I need to move object on certain distance by pressing key once.I have Box Colliders and RigidBodies(Collision detection - Continuous Dynamic) put on walls and charachter.Thanks in advance!