- Home /
moveTowards not effected by maxDistanceDelta
My Object travels to its position as if I've changed the position instead of using moveTowards, despite setting x to anywhere from one trillion to 0.000000000000000001f. I've checked all of the other forum questions but still can't find any answers. Maybe you guys see something I don't.
Transform posA;
Transform posB;
private void Update (){
if (bool == true) {
pos.position = posA.position;
} else {
pos.position = posB.position:
}
transform.position = Vector3.MoveTowards(transform.position, pos.position, x * Time.deltaTime);
}
Could you paste the rest of the code? As in the context to this fragment, where are you calling this?
I am calling this in an update function. The rest of my code is an illegible mess, so I'll just simplify the shown fragment:
Transform posA;
Transform posB;
private void Update (){
if (bool == true) {
pos.position = posA.position;
} else {
pos.position = posB.position:
}
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, pos.position, x * Time.deltaTime);
}
Hi @buddrfingrs,
Your code looks solid to me. Have you tested this simplified code (just these 12 lines or so) in a separate scene, using a separate "test" version of the component, to verify that they work? I often do this when debugging to isolate code. Try doing this and keep simplifying your code until it works. And by "simplifying" I mean getting rid of the if() statement, setting pos.position manually in Start(), setting x manually in Start(), etc.
If the simplified code doesn't work, then both you and I are blindly missing something in the $$anonymous$$oveTowards method here. If the simplified code works, then you know there is something else affecting the transform, in which case I'd need to see more of your code to help figure out an answer.
-Jason
Answer by NVJOB · Jan 23, 2020 at 03:09 PM
This is a fully working script.
Explanatory video - https://www.youtube.com/watch?v=PXa7wMtRkiM
using UnityEngine;
public class Test : MonoBehaviour
{
public bool trigger;
public float speed = 1;
public Transform posA;
public Transform posB;
Vector3 pos;
void Update()
{
if (trigger == true) pos = posA.position;
else pos = posB.position;
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
}
}
It's generally a better style to have the pos variable declaration inside Update if it's not used anywhere else. You could do it easily with the ternary / conditional operator like that
void Update()
{
Vector3 pos = (trigger) ? posA.position : posB.position;
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, pos, speed * Time.deltaTime );
}
Though I assume that if the behaviour that the OP sees is that his object "jumps" to the target position, I assume that his "pos" Transform reference is actually the object he wants to move. Since he directly sets the position of that transform, the object jumps to the target immediately due to the line pos.position = posA.position;
.
I took his code and put it into working form, without structural changes.