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?
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.
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?
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()
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?
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?
@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.