# Unity movement forward doesn't work as expected

firstly i’m sorry for my bad english, i’'ll try to explain my problem…

i’m moving my object on a road builded with a primitive plaines, there is also some downhill parth, (like a car that run on a road)

here is my script :

``````private Transform[] planeArray;
private float speed;
private Quaternion[] _planeRInverse;
private Transform _pvotPlayer;

private void MoveForward ()
{
float moveZ = speed * Time.deltaTime;
_pvotPlayer.transform.position += _pvotPlayer.transform.forward * moveZ;
}

private void SetupPlane()
{
_planeRInverse = new Quaternion[planeArray.Length];
for (int i = 0; i < planeArray.Length; i++)
{
_planeRInverse _= Quaternion.Inverse(planeArray*.rotation);*_
``````

* }*
* } *
private void CheckPosition()
* {*
* Vector3 playerPos = pvotPlayer.transform.position;
_
Vector3 v1;*

* for (int i = 0; i < planeArray.Length; i++)*
* {*
_ v1 = playerPos - planeArray*.position;_
Vector3 lv1 = _planeRInverse _ v1;
float scaleX = planeArray*.localScale.x;_
_float scaleZ = planeArray.localScale.z;
float vh = 5f * scaleX; // unity plane is of 10.0f
float vf = 5f * scaleZ;
if ( IsBetween(lv1.x,0.0f - vh,0.0f + vh) && IsBetween(lv1.z,0.0f - vf,0.0f + vf))
{_

_Vector3 dp = playerPos - planeArray.position;
dp.y = 0;
Vector3 lp = planeArray.rotation * dp;
float fixedY = lp.y + planeArray.position.y + 0.2f;*_

* _pvotPlayer.transform.position = new Vector3(pvotPlayer.transform.position.x,fixedY,pvotPlayer.transform.position.z);*
Vector3 newRot = new Vector3 (planeArray.eulerAngles.x,planeArray.eulerAngles.y,planeArray*.eulerAngles.z);*

* _pvotPlayer.transform.eulerAngles= newRot;*

}
* }*
* }*
* public bool IsBetween(double testValue, double bound1, double bound2)*
{
return (testValue >= Mathf.Min(bound1,bound2) && testValue <= Mathf.Max(bound1,bound2));
}
this should fix my player y axis 0.2f upper on the current plane, but doesn’t work as i’m expected on downhill i’m getting something like this
[126138-immagine.jpg|126138]

anyone know what i’m doing wrong? thank’s you in advance

I dont know what’s wrong with your code. But for this kind of movement you can simply use raycast to get position and rotate your player to the same direction as plane below.

``````	void Update () {
transform.Translate(Vector3.forward * Time.deltaTime*speed);
RaycastHit ray;
if (Physics.Raycast(transform.position, Vector3.down, out ray, 1))
{
transform.rotation = Quaternion.Lerp(transform.rotation, ray.transform.rotation, Time.deltaTime*speed);
Vector3 pos = ray.point;
pos.y += 0.2f;
transform.position = pos;
}
``````