3
I’m a beginner in C#programming, and I’m trying to learn how to make a 2d sidescroller game, but I couldn’t move forward from a part of the tutorial. This is because the camera simply doesn’t follow the character when I press play and Unity shows an error.
I’m following this video (in English), and although I write the same way as the guy in the video, Unity gives this error (happens to each frame of the game when I test, so more than 11000 errors):
Video: Click Here.
The Error the console accused was:
I want to make the camera follow the character of the game:
Script code in c# linked to the camera to follow the character (Camerafollow):
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
private Vector2 velocity;
public float smoothTimeY;
public float smoothTimeX;
public GameObject player;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
}
void FixedUpdate()
{
//O problema diz ser na linha abaixo desse comentário. Eu tentei inverter o posX com o posY, e o erro deu na linha do posY. Também tentei apertar enter e "cortar em vários pedaços" o que está dentro do float posX, o erro parece ser na própria variável posX/posY.
float posX = Mathf.SmoothDamp(transform.position.x, player.transform.position.x, ref velocity.x, smoothTimeX);
float posY = Mathf.SmoothDamp(transform.position.y, player.transform.position.y, ref velocity.y, smoothTimeY);
transform.position = new Vector3(posX, posY, transform.position.z);
}
}
I already deleted the Script and created another with the same code;
I checked all the things that are in the "Hierarchy" tab of the game, none of them have the same Script linked.
Gameobject Player is tagged "Player"?
– João Sobral
Yeah, if tag means his name, he’s
– Kylbert
No, the tag is that
– João Sobral
He was not, I put as Player and now the camera follows, thanks, I can’t believe I was stuck so long to have such a simple solution kkkk
– Kylbert
@Kylbert did not notice that they had already helped him in the comments, I will always write in reply the solution of the questions, even if it finds solution, then answer his own questions, since then it will be able to help other people, in this case it will no longer be necessary, since I already answered and added some relevant information. Now just accept my answer and this question will be resolved. I hope I helped both in Unity and in the advice on SOPT. Greetings!
– Miguel Soeiro
Yes, thanks for the advice and information. As you might see I’m new here so I didn’t know much about running. Your reply was accepted and marked as useful :)
– Kylbert