Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Prefab fails to respond to MoveTowards or RotateTowards.

Discussion in 'Scripting' started by cristo, May 27, 2019.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi, does anyone have any idea why this code isn't working?
    When the player collides with the collider the second camera instantiates at the player's camera location just fine, but then the second camera just freezes and doesn't move toward or rotate towards the goal "Portal Spot".
    Any advice would be great. Thanks for your time.



    Code (CSharp):
    1.   void OnTriggerEnter(Collider player)
    2.  
    3. {
    4.  
    5.         if (player.gameObject.tag == "Player")
    6.         {
    7.        
    8.             Instantiate(CameraThree, CameraPlayer.transform.position, CameraPlayer.transform.rotation);
    9.      
    10.             CameraPlayer.SetActive(false);
    11.      
    12.             CameraThree.transform.position = Vector3.MoveTowards(CameraThree.transform.position, PortalSpot.transform.position, Sspeed * Time.deltaTime);
    13.  
    14.             CameraThree.transform.rotation = Quaternion.RotateTowards(CameraThree.transform.rotation, PortalSpot.transform.rotation, sZpeed * Time.deltaTime);
    15.  
    16.         }
    17. }
    18.        
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    The prefab isn't part of the Scene, the instantiated object is. Don't change the prefab, you should change the newly created (instanced) object. Since you are not retaining the result of the Instantiate call, you can't access the instantiated object. You are currently creating a copy of CameraThree in the Scene (via Instantiate) that you can't access because you never save the resulting reference.

    so your code should look like this:

    Code (CSharp):
    1.  
    2. GameObject newCam = Instantiate(CameraThree, CameraPlayer.transform.position, CameraPlayer.transform.rotation);
    3. CameraPlayer.SetActive(false);
    4.  
    5. newCam.transform.position = Vector3.MoveTowards(CameraThree.transform.position, PortalSpot.transform.position, Sspeed * Time.deltaTime);
    6.  
    7. newCam.transform.rotation = Quaternion.RotateTowards(CameraThree.transform.rotation, PortalSpot.transform.rotation, sZpeed * Time.deltaTime);
    Make sure to move newCam, or save that to the variable that holds the currently active cam.
     
    Last edited: May 27, 2019
    cristo likes this.
  3. dubiduboni_unity

    dubiduboni_unity

    Joined:
    Feb 11, 2019
    Posts:
    116
    I think it's not moving since the MoveTowards line and the RotateTowards line should be inside the Update.
    If they are in the OnTriggerEnter they will be execute only once.
     
    cristo likes this.
  4. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    I really appreciate all your feedback. I tried your code csofranz, thankyou. But the new camera now instantly goes to the PortalSpot, it doesn't translate. Also tried putting things in the update function. Gonna keep trying. :)
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    As dubiduboni already correctly pointed out, you should use Update() to move the new camera, as OnTriggerEnter is only executed once for every time someone enters the Trigger. Use a global Vector3 cameraTargetPosition, set up during OnTriggerEnter, and move the cam towards that Point during update. You can then generalize this later to make the cam go anywhere in your game.
     
    cristo likes this.
  6. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi, thanks for all your feedback csofranz and dubiduboni_unity. I really appreciate it. Thanks to your help it is now working.