I’m currently making a simple runner game. unfortunately, I have a problem with moving my cube to the right. it goes towards a different direction. To give you an idea of what I’m talking about, please check the attached link:
for those who can’t see the image, I hosted it here:
I’ve already edited the code as you suggested, but the same problem persists. it does not move in the right only. it moves in the same direction as the one I showed in the pic. in a fast direction
I also tried editing the speed but still no luck… still moves very fast in an upward direction ( as show in the pic).
any idea what went wrong? o__O
using UnityEngine;
using System.Collections;
public class runner : MonoBehaviour {
public static float distanceTraveled;
public float acceleration;
private bool touchingPlatform;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float distanceToGround = 0 ;
RaycastHit hit;
if (Physics.Raycast(transform.position, -Vector3.up, out hit)){
distanceToGround = hit.distance;
}
if (distanceToGround < 3){
transform.Translate(Vector3.right * 1); //Or any speed
}
}
void FixedUpdate () {
if(touchingPlatform){
rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
}
}
void OnCollisionEnter () {
touchingPlatform = true;
}
void OnCollisionExit () {
touchingPlatform = false;
}
}