P2- CONTROL




1. En la part inferior del programa, en la finestra "Assets" cliquem amb el botó dret, anem a l'apartat "create" i afegim un "C# Scribt". Després anem a la icona i canviem el nom per control.






2. A continuació arrosseguem la icona del nostre control , al nostre avatar.

3. Feu dos clics en la icona de control i canvieu la programació per la següent, en vermell podeu veure les modificacions realitzades:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Control : MonoBehaviour

{

    Vector3 target;

    float speed = 1.0f;

    // Start is called before the first frame update

    void Start()

    {

      SetNewTarget(new Vector3(

        transform.position.x + 10,

        transform.position.y,

        transform.position.x + 10

      ));

    }


    // Update is called once per frame

    void Update()

    {

      Vector3 direction = target - transform.position;

      transform.Translate(direction.normalized * speed * Time.deltaTime, Space.World);

    }

   

    void SetNewTarget(Vector3 newTarget)

{

        target = newTarget;

        transform.LookAt(target);

}

   

}



En el següent vídeo hi ha la explicació de la part superior