SmoothDamp not working perfectly?
So I'm trying to make a sort of drawer that contains items the player can use. The items (which are prefabs) will all be children of an empty object that will move them all together. The code I have sort of works? In that the objects move, but only by a couple of hundreths of a unit, and not in one big movement, instead you have to click the activator object a couple of dozen times to actually see them move at all.
Maybe I'm just doing something really stupid? I've been playing around with this for a few days now and have gotten the example of SmoothDamp in the unity docs to work, but trying my own twist on it doesn't produce the results I want. So here's my code :
var atomSpawner : Transform;
var drawerOpen : boolean;
var startPos : Vector3;
var endPos : Vector3;
var smoothTime = 1;
private var moveSpeed = Vector3.zero;
function Update () {
if(selectedOn == false){
if(hit.collider.tag == "slider"){
atomSpawner.transform.position = Vector3.SmoothDamp(atomSpawner.transform.position, startPos, moveSpeed, smoothTime);
drawerOpen = false;
}
}
else{
if(hit.collider.tag == "slider"){
atomSpawner.transform.position = Vector3.SmoothDamp(atomSpawner.transform.position, endPos, moveSpeed, smoothTime);
drawerOpen = true;
}
}
Any and all help is appreciated!
Answer by phil_me_up · Feb 29, 2016 at 11:12 AM
A couple of potential problems here:
Your setting your current velocity (moveSpeed) to zero. Something else in your code may be changing this but if not, that's a potential problem
SmoothDamp isn't something you call once and wait for it to complete, you need to call it every frame (or at sensible intervals). Your code will only hit SmoothDame in the case that hit.collider.tag equals "slider". Is this going to be the case every frame? Do you ever change the result of hit.collider.tag (I'm assuming this is a member variable which you populate with data at the point of a collision).
SmoothDamp isn't something you call once and wait for it to complete, you need to call it every frame
This was my problem, thanks! I guess I didn't even think about it needing to be called every frame. Now I feel kind of stupid haha.
Your answer
![](https://koobas.hobune.stream/wayback/20220612070024im_/https://answers.unity.com/themes/thub/images/avi.jpg)