Z
Z
Zimaell2020-08-05 14:39:15
Unity
Zimaell, 2020-08-05 14:39:15

How to set movement coordinates for a unit?

I need to put the unit at certain coordinates and pass it the value of where to go

public class Unit : MonoBehaviour{ // прикреплен к префабу
    public Vector3 EndPoint;
    private float speed = 2f;
 
    void Start(){
        //EndPoint= new Vector3(15, 0, 15);
        }
 
    void Update(){
        transform.position = Vector3.MoveTowards(transform.position, EndPoint, speed*Time.deltaTime);
        }
    }
 
public class Spawn : MonoBehaviour{
 
  void Start(){
    SpawnUnit();
    }
 
  void SpawnUnit(){
    Unit Prefab = Resources.Load("Units/unit1", typeof(Unit)) as Unit;
    Instantiate(Prefab, new Vector3(5, 0, 5), Quaternion.identity);
    Prefab.EndPoint = new Vector3(15, 0, 15);
    }
  }

But he does not go to the given coordinates, he goes in the other direction for a couple of cells.
If we remove Prefab.EndPoint = new Vector3(15, 0, 15); and uncomment EndPoint= new Vector3(15, 0, 15); it goes where it needs to go.
But in theory, I already pass it the coordinates Prefab.EndPoint = .... , why doesn't it work like that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-08-05
@Zimaell

Unit Prefab = Resources.Load- this is a link to the resource, in it, in the mind, nothing needs to be changed in runtime.
Instantiate- and here there would be a link to an object in the scene, to which parameters should be set. But you do not save this link.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question