let camera follow player without rotation

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?

You could, in an script atached to the player, put this:

gameObject.transform.position = GameObject.Find("Main Camera").transform.position;

Or just specify an axe like this :

Vector3 cameraPos = GameObject.Find("Main Camera").transform.position;
Vector3 playerPos = new Vector3( GameObject.Find("Main Camera").transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z );
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!

hi when i putt the top code it my player disappears?

You should probably add an offset to the parameters to bring the camera backward.

Like:
Vector3 cameraPos = GameObject.Find(“Main Camera”).transform.position;
Vector3 playerPos = new Vector3( GameObject.Find(“Main Camera”).transform.position.x, gameObject.transform.position.y + 30, gameObject.transform.position.z - 50 );
gameObject.transform.position = playerPos ;

Play with the offset values( +30, -50 ) to match your needs

do i also need to putt that in the player?

Only on the player actually

hi
i have this code right now

using UnityEngine;
using System.Collections;

public class camarafollow : MonoBehaviour {

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update ()
    {
        gameObject.transform.position = GameObject.Find("Main Camera").transform.position;

        Vector3 cameraPos = GameObject.Find("Main Camera").transform.position;
        Vector3 playerPos = new Vector3( GameObject.Find("Main Camera").transform.position.x, gameObject.transform.position.y + 30, gameObject.transform.position.z - 50 );
        gameObject.transform.position = playerPos ;

    }
}

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?

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 :

public Vector3 offset;
void Update ()
    {
        gameObject.transform.position = GameObject.Find("Player").transform.position + offset;
    }

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()

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?

What is your player name on the hierarchy?

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

@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.

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.

Whoops, I skipped over that part, my apologies.
Either way, it’s still better to do it correctly, even in examples imo.