- Home /
SmoothDamp not working?
Hi,
I am moving my object using: transform.position.z-=0.1; because it is set to kinematic... I am trying to make the movement of this object much smoother, searching the WWW I find http://docs.unity3d.com/Documentation/ScriptReference/Mathf.SmoothDamp.html
So I changed the code from that page to match my preferred z axis, see code below:
var target : Transform; var smoothTime = 0.3; var zVelocity = 0.0; var newPosition : float = Mathf.SmoothDamp(transform.position.z, target.position.z, zVelocity, smoothTime); transform.position = Vector3(transform.position.x, transform.position.y, newPosition);
I have this in FixedUpdate() .. When running the code I see the exception: NullReferenceException: Object reference not set to an instance of an object script+$windtimer$54+$.MoveNext () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/script.js:121)
It is failing on line: var newPosition : float = Mathf.SmoothDamp(transform.position.z, target.position.z, zVelocity, smoothTime);
Can anyone tell why this is failing? Or has anyone got some code (or could adapt my code) that might make it easier to smoothly move objects to a new location?
Hmmm, even if I use the unedited code from that Unity reference it give the same error?!
many thanks in advance, Kim
Are you defining zVelocity in the method's scope? You need to define it in the class scope, like in the example given in the documentation.
Hi JoaoOliveira, I tried moving var zVelocity = 0.0; to class level scope, that makes no difference at all.
In the error I notice it shows 54... that is then referring to the position methid/property for "(transform.position.z,", why would that fail? It should know about that I would have thought? Curious.
Like I said before , even if I copy paste that code from the documentation page I see the same error.
I am able to use transform.position.z in various other places in the script/method so I am finding this difficult to believe, understand.
I think the issue might be related top that $$anonymous$$athf.SmoothDamp is expecting float variables but I am using target.position.z; and transform.position.z;
But even if I define these as: var myval1 : float = target.position.z; var myval : float = transform.position.z;
It does not help, but then the error shows on line: var myval1 : float = target.position.z;
So it seems something to do with this?
Answer by Boreale · Jan 28, 2014 at 05:43 PM
Well, considering that null ref is in line
var newPosition : float = Mathf.SmoothDamp(transform.position.z, target.position.z, zVelocity, smoothTime);
and you're running your script on gameObject, the only thing that can be null is target
variable. Make sure 'target' is assigned to your script in inspector or in another script.
That fixed it, newbie mistake I guess, I had var target : Transform; in the method, it wasn't showing in the editor.. and I did not think to assign an object to this. Thanks, working now!
Your answer
![](https://koobas.hobune.stream/wayback/20220613131916im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Smooth moving object 2 Answers
Vector3.SmoothDamp has some invalid arguments 0 Answers
Map level selection slide smoothly 3 Answers
UnityException: Input Axis Rotate2 is not setup. 0 Answers
Lerp a Object along X and Z axis only. 3 Answers