Search Unity

let camera follow player without rotation

Discussion in 'Scripting' started by thebest07111, Oct 24, 2014.

  1. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    hello,

    I want that the camera follow the player. When i use the default script in unity it also follows the rotation.

    I dont want that i only want that it moves over 1 axes so like an 2.5 d and that the camera can only move forward and backward.
    Does anybody knows a code in C# for this?
     
  2. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    You could, in an script atached to the player, put this:

    Code (CSharp):
    1. gameObject.transform.position = GameObject.Find("Main Camera").transform.position;
    Or just specify an axe like this :

    Code (CSharp):
    1. Vector3 cameraPos = GameObject.Find("Main Camera").transform.position;
    2. Vector3 playerPos = new Vector3( GameObject.Find("Main Camera").transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z );
    3. gameObject.transform.position = playerPos ;
    And of course, if it is what you wanted, consider store the GameObject MainCamera at the beginning of your script which will prevent calling GameObject.Find each frame which is costly.

    If you don't know how to do that, ask!
     
  3. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    hi when i putt the top code it my player disappears?
     
  4. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
  5. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    do i also need to putt that in the player?
     
  6. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Only on the player actually
     
  7. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    hi
    i have this code right now
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class camarafollow : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update ()
    13.     {
    14.         gameObject.transform.position = GameObject.Find("Main Camera").transform.position;
    15.  
    16.         Vector3 cameraPos = GameObject.Find("Main Camera").transform.position;
    17.         Vector3 playerPos = new Vector3( GameObject.Find("Main Camera").transform.position.x, gameObject.transform.position.y + 30, gameObject.transform.position.z - 50 );
    18.         gameObject.transform.position = playerPos ;
    19.  
    20.     }
    21. }
    22.  
    this code is on the player
    but for some reason when i start the game the player teleports to -10 39 -72. but why does it do that?
     
  8. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Well my bad, I misread your needs and told you something wrong.
    Atach the script to the Main Camera and not the player.

    And change the Update() for this :
    Code (CSharp):
    1.  
    2. public Vector3 offset;
    3. void Update ()
    4.     {
    5.         gameObject.transform.position = GameObject.Find("Player").transform.position + offset;
    6.     }
    Don't forget to declare the offset Vector3, AND verify that your player has the name Player in hierrachy.

    Then, hit play and try differents values of the offset vector3 in the inspector. When you're ok with your values, enter them in the inspector and you're no longer in play mode or through code. And once again, if it worked, avoid GameObject.Find in update, consider storing the player gameobject in a variable at Awake()
     
  9. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    hi

    I have putt the script you gave me on the main camera and set the camera to the right position. but when i press play the camera doesnt move? did i forget something?
     
  10. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    What is your player name on the hierarchy?
     
  11. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    just like it says in the script Player

    EDIt for some reason it works right now. but the camera is in the player and i cant move the camera to anywhere else? and where do i need to store those posiiton values of the camera?


    EDIT 2: nevermind got it working.

    Thank you very much
     
  12. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    @Diablo404 @thebest07111
    You should NEVER use GameObject.Find in the Update function!
    Why? cause it'll search blindly through all the objects in the scene...

    as quoted from the ScriptReference:
    "For performance reasons it is recommended to not use this function every frame Instead cache the result in a member variable at startup or use GameObject.FindWithTag."
    and
    "This function is most useful to automatically connect references to other objects at load time, eg. inside MonoBehaviour.Awake or MonoBehaviour.Start. You should avoid calling this function every frame eg. MonoBehaviour.Update for performance reasons. A common pattern is to assign a game object to a variable inside MonoBehaviour.Start. And use the variable in MonoBehaviour.Update."

    As stated above, you can cache the found object in a variable.
    What you could also do is create a public GameObject variable and assign the objects in the hierarchy.
    Let me know if u have any questions.
     
  13. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    That's what I said since the beginning. But when you start coding, most of the time you just want your script to work before looking at forbidden and avoidable things. For testing purpose there is nothing wrong using GameObject.Find in Update, it's juste not good practice at all, and not efficient.
     
  14. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Whoops, I skipped over that part, my apologies.
    Either way, it's still better to do it correctly, even in examples imo.