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:
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 argumentsAssets/Scripts/CameraController.cs(51,43): error CS1620: Argument
#3' is missing
ref' modifierAssets/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> ();
//Initialize
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;
}
}
Answer by Glurth · Sep 25, 2015 at 03:35 PM
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.
Answer by Dizy · Sep 25, 2015 at 04:03 PM
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);
Answer by Coetzer · Sep 27, 2015 at 09:15 AM
Thanks so much guys!
$$anonymous$$y code was also a little rigid since I hard coded the Trigger height,
I made the changes suggested by Glurth and also duplicated for downward motion
SO basically the camera only adjusts when player goes above a certain height
using UnityEngine;
using System.Collections;
public class CameraController : $$anonymous$$onoBehaviour {
//what is the player
public PlayerController thePlayer;
//where is the player
private Vector3 lastPlayerPosition;
//camera needs to move
private float distanceTo$$anonymous$$oveX;
// Smooth towards the height of the target
private float cameraTargetHeight;
public float smoothTime;
public float triggerHeight;
private float yVelocity;
private float newHeight;
void Start () {
//what is the player
thePlayer = FindObjectOfType<PlayerController> ();
//Initialize
lastPlayerPosition = thePlayer.transform.position;
}
void Update () {
//equation for camera move
//new position $$anonymous$$us old position
distanceTo$$anonymous$$oveX = thePlayer.transform.position.x - lastPlayerPosition.x;
transform.position = new Vector3(transform.position.x + distanceTo$$anonymous$$oveX, transform.position.y, transform.position.z);
//move camera Y postion
yVelocity = Camera.main.velocity.y;
if (thePlayer.transform.position.y >= (transform.position.y)+triggerHeight) {
newHeight = $$anonymous$$athf.SmoothDamp(transform.position.y, transform.position.y + triggerHeight, ref yVelocity,smoothTime);
transform.position = new Vector3(transform.position.x, newHeight, transform.position.z);
}
else if (thePlayer.transform.position.y <= (transform.position.y)-triggerHeight) {
newHeight = $$anonymous$$athf.SmoothDamp(transform.position.y, transform.position.y - triggerHeight, ref yVelocity,smoothTime);
transform.position = new Vector3(transform.position.x, newHeight, transform.position.z);
}
//find player position
lastPlayerPosition = thePlayer.transform.position;
}
}
Your answer
Follow this Question
Related Questions
Final position of the camera movement 0 Answers
Simple Advice needed - Lerp, SmoothDamp, SmoothStep...... etc. 2 Answers
Coroutine: delay transform rotation but start transform movement immediately 2 Answers
gameobject position lerp 0 Answers
How can you increase a float to certain number and stop, while it is being used. 2 Answers