Moving object form point A to B with triggers?

So what I have been attempting to do (by attempting i mean google-ing all the wrong stuff apparently) is to set up a script which move my cube from point A to B and leaves it there if I step on a button. Anyone know how to do this?

I did it awhile back in some tutorial using empty gameobjects as targets but that was more like a patrol system now after about a year I decided to get back into it i cant remember or find it.

If using rigidBody.
Try this. I didn’t test it since I’m at the office. But it should work i think.

public float speed = 10f;
//Public or Private, set anywhere you want with any value yo want
Vector3 positionA;
Vector3 positionB;

RigidBody myRigidBody;

void Awake()
{
    myRigidBody = GetComponent<RigidBody>();
    myRigidBody.position = positionA;
}

void FixedUpdate()
{
    var movement = Mathf.MoveTowards(transform.position, positionB, Time.deltaTime);
    myRigidBody.MovePosition(transform + movement * speed * Time.deltaTime);
}

void OnTriggerEnter(Collider other)
{
    // check if colliding with the object you want
    if (other.something == somethingelse)
    {
        // This will set positionB to the current position of your gameobject
        // thus making MoveTowars return 0, so it won't move.
        positionB = transform.position;
    }
}

Maybe the second usage of Time.deltaTime should be omitted.

okay getting errors in the following areas:

22,38 The best overloaded method match for ‘unityEngine.mathf.MoveTowards(float, float, float)’ has some invalid arguments.

22,38 Argument ‘#1’ cannot convert ‘UnityEngine.Vector3’ expression to type ‘float’

23,54 Operator ‘*’ cannot be applied to operands of type ‘object’ and ‘float’

23,29 The best overloaded method match for ‘unityEngine.Regidbody.MovePosition (UnityEngine.Vector3)’ has some invalid arguments

23,29 Argument ‘#1’ cannot convert ‘object’ expression to type ‘UnityEngine.Vector3’

using UnityEngine;
using System.Collections;

public class MoveObjects : MonoBehaviour
{

    public float speed = 10f;
    //Public or Private, set anywhere you want with any value yo want
    Vector3 positionA;
    Vector3 positionB;
  
    Rigidbody myRigidBody;
  
    void Awake()
    {
        myRigidBody = GetComponent<Rigidbody>();
        myRigidBody.position = positionA;
    }
  
    void FixedUpdate()
    {
        var movement = Mathf.MoveTowards(transform.position, positionB, Time.deltaTime);
        myRigidBody.MovePosition(transform + movement * speed * Time.deltaTime);
    }
  
    void OnTriggerEnter(Collider other)
    {
        // check if colliding with the object you want
        if (other.gameObject.tag == "Switch")
        {
            // This will set positionB to the current position of your gameobject
            // thus making MoveTowars return 0, so it won't move.
            positionB = transform.position;
        }
    }

}

I fixed the Rigidbody error it was giving before that you spelled Rigidbody as RigidBody.
Any idea what im doing wrong?

Also line 22 var isnt that Javascript?

Sorry. Let’s fix things.

  1. Line 22. Depends on your movement. Is 3D? or 2D? Math.MoveTowards is for 1 direction (should’ve been transform.position.x (or Y)), You can also use Vector2.MoveTowars or Vector3.MoveTowards.

  2. Line 23. My bad. It’s transform.position (again .x or .y in 2D, or using position alone if Vector3)
    And this (movement * speed * Time.deltaTime) should be multiplied by Vector3.Left (or Right Up Down), or Vector3.one)

That should fix all.
var is also available in C#. The only thing that you need to do is asign it a value in the same line.

var a;
a = gameObject;

this is wrong.

But: var a = gameObject. will work.

… I am so sorry I think I just broke it :confused: Seriously my scripting sucks so much can’t get this to work no matter what I try.
Maybe instead of wasting your time I should just learn to script and stop asking stupid questions…

Learning C# and programming altogether will help you greatly.
I could help you if you tell me if you’re working on 2D (moving X or Y or both) or 3D (X or Y or Z or XY or XZ or YZ or XYZ).

But it’s best to first learn some programming basics (any book will help you. Preferably themed to C#). Or you could follow some tutorials if you didn’t already.

Im working on 3D I have watch a few tutorials and I know a few things about scripting such checking the collisions with tags and so on and can create basic movement for the player but other than that im a complete noob :confused: