simple 2d movement to the right

Good day Unity community!

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:

1601639--96732--$cube2.jpg

for those who can’t see the image, I hosted it here:

http://i62.tinypic.com/2hdz76p.jpg

Here is the Unity code I made:

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 () {
transform.Translate(5f * Time.deltaTime, 0f, 0f);
distanceTraveled = transform.localPosition.x;
}

void FixedUpdate () {
if(touchingPlatform){
rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
}
}

void OnCollisionEnter () {
touchingPlatform = true;
}

void OnCollisionExit () {
touchingPlatform = false;
}

}

Question: what went wrong with the code I made? what should I code should I use so that it will move to the side only as shown in the pic?

any response is appreciated. :slight_smile:

Why are you using a rigidbody and with Transform.Translate? Methinks something along these lines would work better:

    void Update() {

         float distanceToGround ;

        RaycastHit hit;
        if (Physics.Raycast(transform.position, -Vector3.up, out hit)){

            distanceToGround = hit.distance;

       }

        if (distanceToGround < 3){

        transform.Translate(Vector3.right * 10); //Or any speed

       }

    }

Hope this helps!

Hello! Thanks for the code!

The reason why I added the rigid body component because my intention is not to let the cube rotate or disappear into the distance.

anyway, I tried to debug the code and I got an error in this line:

if (distanceToGround < 3){

"Use of unassigned local variable ‘distanceToGround’

Could you tell me what’s wrong? did I miss something?

Hoping for your reply.

Oops XD I was in a rush. Replace the

float distanceToGround;

with

float distanceToGround = 0;

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 :frowning:

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;
	}
	
}

All of this code is unnecessary:

    void FixedUpdate () {

        if(touchingPlatform){

            rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);

        }

    }

    

    void OnCollisionEnter () {

        touchingPlatform = true;

    }

    

    void OnCollisionExit () {

        touchingPlatform = false;

    }

Everything is done in the update function :wink: