Make an object move from point A to point B and continue (C#)

How the make an object move between two point continue?
Mean move at axis X,
Example point A 0 and point B 30,
and the object move between A B non stop,
anyone can help?
In C# code.

hi , you can use simply an animation

or

    public float speed;
    public float distance;
    private float xStartPosition;

	// Use this for initialization
	void Start () {
        xStartPosition = transform.position.x;
	}
	
	// Update is called once per frame
	void Update () {

        if (transform.position.x < xStartPosition || transform.position.x > xStartPosition + distance)
        {
            speed *= -1;
        }


        transform.position = new Vector3(transform.position.x + speed * Time.deltaTime, transform.position.y, transform.position.z);
	
	}

:wink:

1 Like

My question to the SCRIPTING FORUM may help you. Still trying to resolve the implementation. Running code once works great. I hope someone can help me with executing the code multiple times.

http://forum.unity3d.com/threads/203278-Scripting-Help-Moving-Player-GameObject-from-POINT-A-to-POINT-B-to-POINT-A

It become just like vibrate, how can it be?

in inspector put in the distance for exemple 30
the speed is in m/sec

Ok, i got it!
Thanks!
But may i know can be make the movement become curve?

you can use math functions like sin , cos…

sorry, i’m a newbie.
can teach me for that?
:slight_smile:

Simple Sin exemple

 public enum Path
    {
        XY, XZ
    }

    public Path path;
    public float speed;
    public float height;
    public int freq;
    public float distance;
    private Vector3 startPosition;

	// Use this for initialization
	void Start () {
        startPosition = transform.position;
	}
	
	// Update is called once per frame
	void Update () {

        if (transform.position.x < startPosition.x || transform.position.x > startPosition.x + distance)
        {
            speed *= -1;
        }

        float x = transform.position.x + speed * Time.deltaTime;

        switch ((int)path)
        {
            case 0:
                transform.position = new Vector3(x, startPosition.y + SinPathFunc(x - startPosition.x), transform.position.z);
                break;
            case 1:
                transform.position = new Vector3(x, transform.position.y, startPosition.z + SinPathFunc(x - startPosition.x));
                break;
        }
	
	}

    float SinPathFunc(float x)
    {
        float y;
        y = height * Mathf.Sin((x * Mathf.PI) / (distance / freq));
        return y;
    }

Its work, but if i wanna stop the movement and make it fall down,
it din’t work.
i add the

void OnMouseDown()
{
gameObject.rigidbody.addForce(0,10,0);
}
 void OnMouseDown()
    {
        speed = 0;
        Rigidbody rb = gameObject.AddComponent<Rigidbody>();
        rb.freezeRotation = true;
        rb.useGravity = true;
    }

the object just become freeze,
din have any gravity happen.

Any help for make it fall down??

how a position can be change with respect to others means as one goes to a position then other move