Problem with the Unity camera

Posted on

Question :

I’m creating a game with unity 5 in 2d, and tried to create a script for the camera to follow the character by moving the camera transform.
For this I followed some tutorials on youtube but when I was compiling the code indicated in the Lerp method, I tried to adjust the code in several ways and I did not get a solution, which I send here and the last version of the code that I have changed infinitely many times.


Answer :

As the error message indicates, your call to the Lerp method expects parameters other than those you passed on. It expects two Vector3 ( a and b ) and float ( t ), because what this method does is interpolate between two vectors a and b in a t .

You are passing in the first parameter, for example, MCamera.position.x , which is the x component (a float value) of the position vector. You should probably pass MCamera.position directly (and the same goes for the parameter transform.position.x , which should be only transform.position ).

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


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


An alternative would simply be to drag the House to in (child) of the Character. Thus, whenever the character moves, the camera goes behind without needing to write a single line of code.


Leave a Reply

Your email address will not be published. Required fields are marked *