- Home /
Sliding game slope jittering.
Im making a sliding game and i want to achieve something very similar to this, Where The character movement is limited to moving side to side. Rotation and forward movement is controlled automatically.
Im using iTween to do this. There is one object that moves on the path and orients to it, the second object is its child and it controls the side to side movement. The third object is following the second one and getting its rotation on the terrain. The problem is, when the third object is going down a slope it jitters.
There is the code.
Object1
void Start () {
iTween.MoveTo(gameObject, iTween.Hash("path", iTweenPath.GetPath("Path1"), "time", 15, "easetype", iTween.EaseType.linear, "orienttopath", true));
}
Object2
public float speed;
public float moveHorizontal;
public float drag;
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
moveHorizontal = -1;
}
else if (Input.GetKey(KeyCode.RightArrow))
{
moveHorizontal = 1;
}
else if (Input.GetKeyUp(KeyCode.LeftArrow))
{
moveHorizontal = 0;
}
else if (Input.GetKeyUp(KeyCode.RightArrow))
{
moveHorizontal = 0;
}
rigidbody.drag = drag;
if (moveHorizontal == 0)
{
drag = 10;
}
else
{
drag = 0;
}
}
void FixedUpdate()
{
Vector3 movement = new Vector3(moveHorizontal, 0.0f, 0.0f);
rigidbody.AddRelativeForce(movement * speed * Time.deltaTime);
transform.localPosition = new Vector3(transform.localPosition.x, 0, 0);
}
Object3
public GameObject obj;
void Start ()
{
obj = GameObject.Find("Object2");
}
public float speed;
void LateUpdate()
{
Vector3 target =new Vector3(obj.transform.position.x,transform.position.y,obj.transform.position.z);
transform.rotation = Quaternion.Euler(transform.eulerAngles.x,obj.transform.eulerAngles.y, transform.eulerAngles.z);
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position,target, step);
}
Im an absolute begginer and i have no idea if it is the correct way to do this.
Comment