- Home /
Lerp to a position while rotating around
Hi there,
I am making a racing-type game with tracks that have curves. I currently have the player lerping between each "lane" whenever they press down on the arrow keys. This works fine for translating down the z-axis, but not for any other direction. My question is, how do I lerp the player (using local positions) when the player's transform.forward is no longer on the world z-axis?
This represents the easy and working code that I have. Only working if the player is traveling down the z-axis.
And this represents the turns in my game. I can't figure out how to keep it lerping to a position that is a certain distance away, while keeping it updated through the turn.
Here is my code: using UnityEngine; using System.Collections;
public class Player : MonoBehaviour
{
private Camera _mainCam;
public bool[] _lanes = new bool[7]
{
false,false,false,false,false,false,false
};
public int _currentLane = 3;
public Vector3 _newLane;
public bool _changingLane = false;
public bool _moveLeft;
private bool _inGame = false;
//public int playerHealth;
public const float BASE_SPEED = 20f;
public float _currentSpeed;
public float _strafeSpeed = 20f;
public bool inGame
{
get
{
return _inGame;
}
set
{
_inGame = value;
}
}
// Use this for initialization
void Start ()
{
_mainCam = Camera.main;
// Only set the middle lane true before game starts
doSetLane(7, true);
_currentSpeed = 0;
}
// Update is called once per frame
void Update()
{
this.transform.position += this.transform.forward * Time.deltaTime * 10f;
if(!_changingLane)
{
_listenForMovement();
}
else
{
_doChangeLane();
}
}
private void _listenForMovement()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
if(_currentLane == 0)
{
Debug.Log("Can't move left.");
}
else
{
--_currentLane;
_moveLeft = true;
_changingLane = true;
_newLane = this.transform.TransformPoint (new Vector3(-1.5f, 0, 0));
Debug.Log("Moved left");
}
}
if (Input.GetKey(KeyCode.RightArrow))
{
if(_currentLane == _lanes.Length - 1)
{
Debug.Log("Can't move right.");
}
else
{
++_currentLane;
_moveLeft = false;
_changingLane = true;
_newLane = this.transform.TransformPoint (new Vector3(1.5f, 0, 0));
Debug.Log("Moved right");
}
}
}
/**
* public void doSetLane ( int, bool )
*
* @description
* Sets the lane(s) provided to the set condition.
* This tells the game if the player can travel the lane or fall through.
*
* @param index {int} Lane to be changed
* @param condition {bool} Turn the lane on or off
*/
public void doSetLane(int index, bool condition)
{
if (index >= _lanes.Length)
{
for (int i = 0; i < _lanes.Length; ++i)
_lanes [i] = condition;
}
else if (index >= 0 && index < _lanes.Length)
{
_lanes [index] = condition;
}
else
{
Debug.Log("Player.cs@doSetLane(int,bool): Lane index out of range! index == " + index);
}
}
/**
* public void _doMovePlayer ()
*
* @description
* Moves the player left or right.
*/
private void _doChangeLane()
{
//Vector3 tmp = this.transform.position;
_newLane.z = this.transform.position.z;
if(_moveLeft)
{
this.transform.position = Vector3.Lerp(this.transform.position, _newLane, _strafeSpeed * Time.deltaTime);
}
else
{
this.transform.position = Vector3.Lerp(this.transform.position, _newLane, _strafeSpeed * Time.deltaTime);
}
/*Debug.Log ("x: " + (this.transform.position.x + (this.transform.position.x - tmp.x)));
Debug.Log ("z: " + (this.transform.position.z + (this.transform.position.z - tmp.z)));
_newLane.x = this.transform.position.x + (this.transform.position.x - tmp.x);
_newLane.z = this.transform.position.z + (this.transform.position.z - tmp.z);*/
if(Vector3.Distance(this.transform.position, _newLane) <= 0.01f)
{
this.transform.position = _newLane;
_changingLane = false;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613163521im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I smooth out my crouch camera movement 0 Answers
Mathf.Lerp Question 2 Answers
Smooth swapping my game object 0 Answers
Need to check transform.rotation 1 Answer
Learning to move 1 Answer