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

Follow object with Code

Discussion in 'Scripting' started by larswik, Sep 21, 2015.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Hey,
    I am learning Unity but have C and objective C programming experience. I have a test going on where I imported the FPSController asset for a first person camera. I then grabbed a tif image of a cockpit as a texture and placed in on a 3D plane asset in front of the camera. When I place the Plane as a child of the camera asset it follows the transform of the camera.

    I then decided to test this with code and remove the Plane a child object and added a new script to the Plane asset.

    With this code it it is causing the FPSController asset to start getting pushed away at a high rate of speed. I have no control of the forward momentum and can not stop it. I'm assuming that the plane asset is snapping to the camera which causes the camera to move, since it updates every frame it keeps pushing the camera back.

    Code (CSharp):
    1. public class CameraFollow : MonoBehaviour {
    2.  
    3.     public GameObject cameraObj;
    4.  
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.         var pos = new Vector3 (cameraObj.transform.position.x ,cameraObj.transform.position.y ,cameraObj.transform.position.z);
    14.         transform.position = pos;
    15.  
    16.         Debug.Log (pos);
    17.         //transform.Translate(Vector3.forward * Time.deltaTime);
    18.     }
    19. }
    If the camera is not moving there should be no transform.position data to apply to the Plane? So why is it moving? I included an image to better understand what I am doing.
     

    Attached Files:

  2. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    This seems like an easy one so I am bumping the message so it won't get lost in the stack.
     
  3. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    You need to word your questions better, I had to read it 5 times to completely understand what was happening.

    "placed in on a 3D plane asset in front of the camera"

    A plane is flat surface in 3D space. But you specify a "3D Plane". Front of camera could mean anything from X,Y or Z.

    Anyhow the reason this is happening is due to your Plane trying to move to your cameraObj Transform while the camera is being pushed back by the plane the plane updates to the camera Transform(Viscus Cycle Syndrome). :)

    Add a public or static float value between the plane and camera like so.
    Code (CSharp):
    1. cameraObj.transform.position.z + 10f
     
    Last edited: Sep 22, 2015
  4. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Thank you! that was it. I simply added +1f to this line and it fixed it.
    Sorry about my terminology. if felt odd getting a 2D plane object from the 3D create menu, I can see how that confused people.
     
  5. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    Hey, no problem I'm glad I could help.