Search Unity

what do I miss with MoveTowards?

Discussion in 'Scripting' started by S-dieters, Jul 26, 2014.

  1. S-dieters

    S-dieters

    Joined:
    Aug 27, 2013
    Posts:
    42
    Hey all,
    here is the situation:
    I have one Main Camera, and 3 Dummy Camera's which are only used to for it's transform. now my goal is to change my Main Camera's transform to the one of the selected Dummy Camera (by pressing 1, 2 or 3). the Main Camera moves for one frame into the right direction, but stops immediately , as if it will only move for one frame. On first glance I thought it happened because the next frame " i " was set to 0 the next frame, posibly meaning he forgets his target position, but even when I push the value of " i " over to targetCam in my MoveCamera function, it still doesn't work.

    So do I miss something, or do I simply misunderstand the MoveTowards function?
    P.S, I rather like to have an explanation of how stuff works, instead of someone telling me the answer straight away. Otherwise I still have no clue what I did wrong hehe.

    Cheers!

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveCameraTo : MonoBehaviour {
    5.  
    6.     public GameObject[] cameras;
    7.     public string[] shortcuts;
    8.     public float speed = 5;
    9.  
    10.     void Update () {
    11.         for (int i = 0; i < cameras.Length; i++){
    12.             if(Input.GetKeyDown(shortcuts[i])){
    13.                 MoveCamera(i);
    14.             }
    15.         }
    16.     }
    17.  
    18.     void MoveCamera (int index){
    19.         Debug.Log (index + " was pressed");
    20.         int targetCam = index;
    21.         transform.position = Vector3.MoveTowards(transform.position, cameras[targetCam].transform.position, speed*Time.deltaTime);
    22.     }
    23. }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    GetKeyDown is only true for the single frame that the key is pressed down, so you only call the MoveCamera function once. You need to call MoveTowards every frame if you want it to work.

    --Eric
     
    S-dieters likes this.
  3. S-dieters

    S-dieters

    Joined:
    Aug 27, 2013
    Posts:
    42
    That I understand, but I thought that once you pressed a key, MoveTowards will move an object until it reaches it's destination. How should I build the code that if you press the key once, it will move the camera all the way to it's new position?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, MoveTowards, like other functions such as Lerp, doesn't "do stuff over time" or anything. It returns a value once, immediately. As I mentioned, you need to call it repeatedly in Update (or a coroutine).

    --Eric
     
    S-dieters likes this.
  5. S-dieters

    S-dieters

    Joined:
    Aug 27, 2013
    Posts:
    42
    so if i am correct (i'm just trying to understand the way all functions work), it goes something like ...

    Vector3.MoveTowards(A, B, C)

    if the current position of whatever object this script is attached to hasn't reached position B yet, it will calculate how much the object has moved between point A and B at a certain speed (C) after one frame.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Pretty much, except C is technically a distance, not speed.

    --Eric
     
    S-dieters likes this.
  7. S-dieters

    S-dieters

    Joined:
    Aug 27, 2013
    Posts:
    42
    thank you very much!