Smooth camera to target Y-height

Hay guys,

I’m having some troubles with my MathF statements, could someone pleaaaase help me out, still pretty new to C# and getting some errors I don’t understand

My errors are:

  1. Assets/Scripts/CameraController.cs(51,43): error CS1502: The best overloaded method match for `UnityEngine.Mathf.SmoothDamp(float, float, ref float, float)’ has some invalid arguments
  2. Assets/Scripts/CameraController.cs(52,112): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Vector3(float, float, float)’ has some invalid arguments
  3. Assets/Scripts/CameraController.cs(51,43): error CS1620: Argument #3' is missing ref’ modifier
  4. Assets/Scripts/CameraController.cs(52,112): error CS1503: Argument #2' cannot convert UnityEngine.Vector3’ expression to type `float’

Thank you!

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

	//what is the player

	public PlayerController thePlayer;

	//where is the player

	private Vector3 lastPlayerPosition;

	//camera needs to move

	private float distanceToMoveX;

	// Smooth towards the height of the target
	private float cameraTargetHeight;
	public float smoothTime;
	public float yVelocity;

	private Vector3 newHeight;

	void Start () {

		//what is the player
		thePlayer = FindObjectOfType<PlayerController> ();

		lastPlayerPosition = thePlayer.transform.position;


	void Update () {

		//equation for camera move
		//new position minus old position

		distanceToMoveX = thePlayer.transform.position.x - lastPlayerPosition.x;

		transform.position = new Vector3(transform.position.x + distanceToMoveX, transform.position.y, transform.position.z);

		//move camera Y postion

		yVelocity = Camera.main.velocity.y;

		if (thePlayer.transform.position.y >= 4) {

			newHeight = Mathf.SmoothDamp(transform.position.y, transform.position.y + cameraTargetHeight,yVelocity,smoothTime);
			transform.position = new Vector3(transform.position.x, newHeight, transform.position.z);	

		//find player position

		lastPlayerPosition = thePlayer.transform.position;

1&2: In your smooth damp function, your third parameter is the “current speed”, which is stored in the yVelocity variable you have. The smoothdamp function will need to MODIFY this varaible, so it is passed as a Reference. This means you need to add REF right in front of the variable name, to pass a “reference” to it, as opposed to just it’s value.

newHeight = Mathf.SmoothDamp(transform.position.y, transform.position.y + cameraTargetHeight,ref yVelocity,smoothTime);

Edit: also- you are assigning the return value of this function to a Vector3 variable, not a float. Perhaps you meant to define newHeight as a float?

1 & 3: Your Vector 3 function needs to take three float values. but your newHeight value is Vector3.

It seems that you are trying to follow a character on a 2D game, or something like.

It’s a long way to move a cam on Y toward a new target, you can use this if you want to try (not tested) :

var step = speed * Time.deltaTime;
Vector3 newPosition = new Vector3(transform.position.x, thePlayer.position.y, transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, newPosition, step);

Thanks so much guys!