Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Camera follow instantiate prefab?

Discussion in 'Scripting' started by Bongmo, Mar 2, 2015.

  1. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
  2. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    Camera.main.transform.position controls the position of the camera.

    You Can also make the camera a Child of the bullet, aand position it in the editor.
     
  3. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    I tried that before. The problem is here, that when the bullet touches the ground and moves and rotates, the camera rotates too.
     
  4. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    If it rotates, the u.must have made it a child.

    Use the code aproach i said in my last post. That will only change the position, not the rotation.
     
  5. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    First you got to store the instantiated object in a private varible. Then you are going to lerp Camera's transform to a position which is behind the bullet, same X-Y axis, but only Z axis changes.

    Code (CSharp):
    1. private Transform toFollow;
    2. private bool canFollow;
    3. public float lerpSpeed;
    4. public float distance;
    5.  
    6. void SomeFunction()
    7. {
    8. // Your Instantiate function
    9. Transform instantiatedObj = Instantiate() as Transform. // whatever your parameters are.
    10. toFollow = instantiatedObj;
    11. canFollow = true;
    12. }
    13.  
    14. void Update()
    15. {
    16. if(canFollow)
    17. {
    18. Vector3 followPos = new Vector3(toFollow.position.x, toFollow.position.y, toFloow.position.z - distance);
    19. transform.position = Vector3.Lerp(transform.position, followPos, Time.deltaTime * lerpSpeed);
    20. }
    21.  
    22. }
    Attach this to camera and it will make it follow the instantiated object with a distance.

    Note that this script will only keep the position behind it, rotation of the camera will stay still, you can use LookAt or Quaternion.LookRotation to make the camera to rotate according to bullet, or just simply child the camera to the bullet, but beware, any problemed rotation changes in bullet will affect the camera much more.
     
  6. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    @lineupthesky:
    EDIT:
    It's not working. The camera is at the same position like the instantiated object. I can't see the object, because it's the same camera position.
     
  7. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Did you tweak the distance parameter in the inspector ? Also, I noticed that I've made a typo, toFloow.position.z, instead of toFollow.position.z, I think you already fixed it ?
     
  8. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    Yes. I did that. I think, the "toFollow.position.z - distance" is not working.
     
  9. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Dude now I tried it in case of anyting, it works just fine, there must be a problem about your setup :

    - Check Distance Parameter
    - Check Lerp Speed Parameter
    - Debug the toFollow object to see if it's getting equal to your last instantiated prefab.
     
  10. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    No, it's not working. I don't know, where is the problem. Here are my variables.
    public float lerpSpeed = 105f; // this big value is also not working
    public float distance = 1f;
     
  11. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    This is working, but the object and the camera has the same position. I can't see the object. Why is the camera not behind the object?

    Code (CSharp):
    1. Vector3 followPos = new Vector3(toFollow.position.x, toFollow.position.y, toFollow.position.z - distance);
    2. transform.position = followPos;
    With this code, the camera must be behind the object, right?
     
  12. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    @lineupthesky: It's now working. Thank you.
    What's the problem with unity?

    This was the problem:
    public float lerpSpeed = 10f;
    public float distance = 1f;

    This variables are public. I changed the variables in the code. The variables in the inspector were 0. Unity doesn't updated the public variables.
     
    lineupthesky likes this.
  13. easterbunnie228

    easterbunnie228

    Joined:
    Jan 31, 2017
    Posts:
    2
    public variables don't update on their own if they are already attached to a game object. Hit the little grey cog icon in the script section of the inspector and then hit refresh, to refresh the variables.