Hello,
Im making a space game, where the ship can only travel in 3 lanes (left, middle, right - like an endless runner game), my question is how do i rotate the ship while moving in lanes? Like from Left to middle it should rotate on Z by -40 and when arrived it should be on 0 again. Can some1 fill up my script with the rotation part? Or suggest anything that could help? Thank you for your time and sorry for posting this here, i coudnt make the answer section work.
Here is my movement script:
private const float LANE_DISTANCE = 20f;
public float speed = 7.0f;
private float gravity = 12f;
private CharacterController controller;
private float verticalVelocity;
private int desiredLane = 1; // 0 = Bal, 1 = Közép, 2 = Jobb
private void Start()
{
controller = GetComponent<CharacterController>();
}
private void FixedUpdate()
{
// Mozgás balra
if (Input.GetKeyDown(KeyCode.A))
MoveLane(false);
// Mozgás jobbra
if (Input.GetKeyDown(KeyCode.D))
MoveLane(true);
// Kiszámolja, hogy a JÖVŐBEN hol kell lennünk.
Vector3 targetPosition = transform.position.z * Vector3.forward;
if (desiredLane == 0)
targetPosition += Vector3.left * LANE_DISTANCE;
else if (desiredLane == 2)
targetPosition += Vector3.right * LANE_DISTANCE;
// Move delta
Vector3 moveVector = Vector3.zero;
moveVector.x = (targetPosition - transform.position).normalized.x * speed;
// Calculate Y
if (IsGrounded())
{
verticalVelocity = -0.1f;
}
moveVector.y = -0.1f;
moveVector.z = speed;
// Űrhajó controller
controller.Move(moveVector * Time.deltaTime);
}
private void MoveLane(bool goingRight)
{
desiredLane += (goingRight) ? 1 : -1;
desiredLane = Mathf.Clamp(desiredLane, 0, 2);
}
private bool IsGrounded()
{
Ray GroundRay = new Ray(
new Vector3(
controller.bounds.center.x,
(controller.bounds.center.y - controller.bounds.extents.y) + 0.2f,
controller.bounds.center.z),
Vector3.down);
return false;
}
}