- Home /
MathF.SmoothDamp
Hey, I'm trying to simply translate the camera from one position to the next. I'm getting the error;
Assets/Scripts/InGameGUI.js(471,46): BCE0023: No appropriate version of 'UnityEngine.Mathf.SmoothDamp' for the argument list '(UnityEngine.Vector3, UnityEngine.Vector3, float)' was found.
However from my end, the code looks sound.
Anyone have any ideas of what I've missed?
function CameraStart ()
{
var camStartPosition = Vector3 (11.5, 0, -5);
var camEndPosition = Vector3 (0, 0, -5);
transform.position = camStartPosition;
var getCamSpeed : int = 0;
getCamSpeed = camSpeed;
getClock.GetComponent(clock).playTimeEnabled = false;
camSpeed = 1.0;
yield WaitForSeconds (7);
transform.position = Mathf.SmoothDamp(camStartPosition, camEndPosition, camSpeed * Time.deltaTime);
//moveCamCenter = true;
yield WaitForSeconds (5.5);
getClock.GetComponent(clock).playTimeEnabled = true;
camSpeed = getCamSpeed;
}
Answer by Bunny83 · Nov 24, 2013 at 10:36 PM
It seems you mixed up Mathf.SmoothDamp with Vector3.SmoothDamp.
Mathf.SmoothDamp works only on single float values while Vector3.SmoothDamp works on vector3s.
hey thanks for the info Bunny!
Unfortunately, when I changed the line to;
transform.position = Vector3.SmoothDamp(camStartPosition, camEndPosition, camSpeed * Time.deltaTime);
I just recieved a different error;
Assets/Scripts/InGameGUI.js(471,48): BCE0023: No appropriate version of 'UnityEngine.Vector3.SmoothDamp' for the argument list '(UnityEngine.Vector3, UnityEngine.Vector3, float)' was found.
:(
Well, SmoothDamp needs at least 4 parameters and yours are actually quite wrong.
The first parameter is your current position so you have to pass
transform.position
The second parameter is the target you want to reach. This should probably be your
camEndPosition
For the third parameter you have to pass a velocity vector which is modified by the function. It uses this variable to store the current velocity. You just have to pass a Vector3 variable
The forth parameter is the smooth time. You shouldn't multiply this value by Time.deltaTime. It's the approximate time in seconds.
The last two parameter doesn't have to be specified. maxSpeed could be used to avoid a velocity greater than this value. By default it's infinity, so if necessary the function could crank up the velocity up to "ludicrous speed". Finally there's deltaTime which by default is, well, Time.deltaTime ;)
So far about what you have to pass to the function. The next thing is the function has to be called each frame to work properly. It seems you call it just once.
Another thing is you have an int variable getCamSpeed but assign a float value to it. $$anonymous$$eep in $$anonymous$$d that you will loose the fraction when you do this.
There's way to much wrong in your code and it's hard to say what's the actual behaviour you're after.
It's quite late here (03:00), so i'm off ;)
Good luck
Thanks for checking back with me Bunny. I checked up on the API regarding Vector3.SmoothDamp. And made some alterations to the code as you suggested.
private var camStart: boolean = true;
function Update
{
if (camStart)
{
CameraStart ();
camStart = false;
}
}
function CameraStart ()
{
var camStartPosition = Vector3 (11.5, 0, -5);
var camEndPosition = Vector3 (0, 0, -5);
var velocity = Vector3 (-5, 0, 0);
transform.position = camStartPosition;
var getCamSpeed : int = 0;
getCamSpeed = camSpeed;
getClock.GetComponent(clock).playTimeEnabled = false;
camSpeed = 5.5;
yield WaitForSeconds (7);
transform.position = Vector3.SmoothDamp(camStartPosition, camEndPosition, velocity, camSpeed );
//moveCamCenter = true;
yield WaitForSeconds (5.5);
getClock.GetComponent(clock).playTimeEnabled = true;
camSpeed = getCamSpeed;
}
However the end result was the camera either stopped moving after 1 frame, or jittered....depending if I had the camStart boolean at the end of the function itself, or after the CameraStart call in function Update (). (I'm unsure why I would get different results depending , as it should equal the same thing in either place?)
The goal of this code was to simply have the camera start on camStartPosition, hold for several seconds, then pan smoothly over to the camEndPosition.
I was currently using Vector3.Lerp to move the camera from one position to the next, but I dislike the slow into the camEndPosition that seems to be the trademark of using a Lerp.
I will try to dig into this code and see if there isn't anything else fighting with it.
Thanks.
I don't know if you're still fiddling with this, but you are missing a time argument in your smoothDamp and you aren't feeding in the current position. You currently have:
Vector3.SmoothDamp(camStartPosition, camEndPosition, velocity, camSpeed );
The way you've written this asks the camera to move to a specific point in between camStartPosition and camEndPosition, rather than move between them. What you want is:
Vector3.SmoothDamp(transform.position, camEndPosition, velocity, Time.deltaTime * camSpeed );
Think of it this way: Vector3.SmoothDamp( whereAmI, whereAmIHeaded, how$$anonymous$$uchWasI$$anonymous$$ovingLastFrame, how$$anonymous$$uchShouldITryTo$$anonymous$$oveThisFrame * timeSinceLastFrame );
If you want to delay the start of the move, animate the value for camSpeed. If camSpeed = zero, the camera won't move at all. Then just ease into whatever speed you want.
Answer by leventalpsal · Oct 16, 2015 at 10:02 AM
Hi, @Ryan got point, you are giving the start position, you should give the current position (which is the same as start position only at the start ) and apply that return value to current position.
You must also give the velocity parameter as reference so that the function can modify it and use the new velocity value next time.
And the 4th param should represent time as float. Something that won't change.
Also execution order is not correct I think.
So it should be something like this : (c#)
void camStart() { float smoothTime = 5.5f; Vector3 camStartPosition = new Vector3 (11.5, 0, -5); var camEndPosition = new Vector3 (0, 0, -5); var velocity = new Vector3 (-5, 0, 0); transform.position = camStartPosition;
void update() { if(cameraStarted) { transform.position = Vector3.SmoothDamp(transform.position, camEndPosition, ref velocity, smoothTime ); } }
Didn't I mention that in my comment that is 2 years old? Ryan just repeated some of my points but got the Time.deltaTime wrong again. His post is actually one year old....
@Bunny83 I've mentioned multiple points which one are you saying? Yes you have mentioned to call the function in every frame, but there is the "reference" detail that hasn't been mentioned before by anyone.
This won't work:
transform.position = Vector3.SmoothDamp(transform.position, camEndPosition, velocity, smoothTime );
This will:
transform.position = Vector3.SmoothDamp(transform.position, camEndPosition, ref velocity, smoothTime ); // ref velocity
Your answer
Follow this Question
Related Questions
Time goes wrong in the Build 1 Answer
Mathf.SmoothDamp happens instantly 1 Answer
My rotation affects a value it should not. 0 Answers
SmoothDamp from 360 to 1 1 Answer