Problem with the Unity camera

Asked

Viewed 1,010 times

4

I am creating a game with Unity 5 in 2d, and I was trying to create a script for the camera to follow the character moving the camera Transform. For this I followed some tutorials youtube but when I went to compile the code indicated in the Lerp method, I tried to adjust the code in several ways and I did not get solution, what I send here and the last version of the code I have changed endlessly giving no result.

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • Hello. Welcome to SOPT. First of all, there is a tip for other questions in the future: do not post images with screenshots of your code. Post the code in textual form (properly formatted automatically by the system)! So anyone interested can just copy a snippet without having to type it all in manually.

2 answers

4

As the error message indicates, your method call Lerp expects different parameters from the ones you passed. It expects two Vector3 (a and b) and a float (t), because what this method does is interpolate between two vectors a and b at a time t.

You are passing the first parameter, for example, MCamera.position.x, which is the component x (a value float) vector position. You should probably come by MCamera.position directly (and the same goes for the parameter transform.position.x, that should be just transform.position).

If your interest is to interpolate only the value of x even, experiemente with Vector3(transform.position.x, 0, 0) (creates a new vector with only its value x, and with the value 0 for the components y and z).

P.S.: You should also make the last line be MCamera.position = vetor instead of MCamera.position.x = vetor.

0

An alternative would be to simply drag the Chamber to within (Child) of the Character. Thus, whenever the character moves, the camera goes behind without writing a single line of code.

Processo de hierarquização: passagem da câmara para objeto-filho da personagem

  • Hello. It’s a really great alternative. But your answer could have some additional information. You could, for example, include images detailing the camera’s hierarchical process as the character’s child object, to make it easier for an inexperienced reader. If you do that you get my +1. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.