I'm very new need help with movement

I need to know how to make an object move between two points independently.

There are many ways to approach it depending on exactly what you are trying to do. Most methods involve setting transform.position, but beyond that you’re going to have to explain more about what you are trying to do.

It sounds like you will want to have a variable called t that is between 0 and 1.

When t is 0, it will be at point 1. When t is 1, it will be at point 2.

x3 = x1 * (1-t) + x2 * t;
y3 = y1 * (1-t) + y2 * t;
z3 = z1 * (1-t) + z2 * t;

You may want to look at this:

https://unity3d.com/learn/tutorials/topics/scripting/linear-interpolation

Here is some code from that page:

Vector3 from = new Vector3 (1f, 2f, 3f);
Vector3 to = new Vector3 (5f, 6f, 7f);

// Here result = (4, 5, 6)
Vector3 result = Vector3.Lerp (from, to, 0.75f);

So, basically, that last parameter is the t I was talking about. You’ll want to vary that between 0 and 1.

Here’s some code to move back and forth between two points:

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {

    // Use this for initialization
    void Start () {
  
    }


  
    // Update is called once per frame
    void Update () {
        Vector3 point1 = new Vector3(-6f, 1f, 0);
        Vector3 point2 = new Vector3(6f, 1f, 0);
        float t = Time.time - (int)Time.time;
        if ((int)Time.time % 2 == 0)
            t = 1f - t;
        Vector3 point3 = Vector3.Lerp (point1, point2, t);
        transform.position = point3;
  
    }
}

https://www.youtube.com/watch?v=QYM_Cl5QCtU

1 Like

ty very much :slight_smile: you even made a video lol, oh and what should I change so it translates with the speed I want