- Home /
How do I make my Player move?
Hello, I have 2.5D game in a 3D project and after completing a goal the player is supposed to move (automatically) to a certain position, which would be -84.88f, -0.7f, -7.24f. I've tried many tutorials online however none of them work with my game. If I try the most commonly used childTransform.localPosition = Vector3.MoveTowards(childTransform.localPosition, nextPos,speed * Time.deltaTime);
with empty objects then my character flies away. I also tried using NavMesh agents, but those did not work either. My character just does nothing (let it be said, just in case that I am 100% positive that my empty object aka the position where the character should end up is at the correct place. However, it just flies away or sometimes does other weird things.
The only solution that has worked was this one: playerTransform.transform.position = new Vector3(-84.88f, -0.7f, -7.24f);
However, then my player just transforms to this place, without moving there. Is there anyone that could help me with this issue? Thank you! I'll add more details if necessary.
Answer by kalen_08 · Jul 20, 2018 at 12:36 AM
The method you're using doesn't care about local positioning. If objA and childA are both at (0,100,0). ChildA local position = 0; if you say move towards and use the local position the it will read as childA.MoveTowards(0, -100, speed); Eventually the child will end up at -200 in world space. Use transform.position instead
childTransform.position = Vector3.MoveTowards (childTransform.position, nextPos, speed * Time.deltaTime);
Always use World Space when dealing with multiple objects. (ie. transform.position not transform.localPosition)
here is how i do it:
public class MoveToTarget : MonoBehaviour
{
[SerializeField] Transform targetTransform = null;
[SerializeField] float speed = 1;
[SerializeField] float stoppingDistance = 0.01f;
void Start ()
{
StartCoroutine(MoveToObjectPosition(targetTransform));
}
IEnumerator MoveToObjectPosition (Transform location)
{
while (location != null)
{
transform.position = Vector3.MoveTowards (
transform.position,
location.position,
speed * Time.deltaTime
);
float distanceToTarget = Mathf.Abs(Vector3.Distance(transform.position, location.position));
yield return new WaitForEndOfFrame();
if (distanceToTarget <= stoppingDistance) {
yield break;
}
}
}
}
Hope that makes sense let me know :)
Hello, thank you for taking your time and answering to my question, however the problem is my character just flies away when I use your code. It just starts flying and not moving. It doesn't even fly to the direction it's supposed to. Here's the code that I used:
private Transform childTransform;
private Transform transformB;
private Vector3 velocity = Vector3.zero;
public float speed;
private Vector3 posA;
private Vector3 posB;
private Vector3 nextPos;
void Start () {
posA = childTransform.localPosition;
posB = transformB.localPosition;
nextPos = posB;
if( _accomplished == 0 )
{
childTransform.position = Vector3.$$anonymous$$oveTowards(childTransform.position, nextPos, speed * Time.deltaTime);
The only thing that works is:
//childTransform.transform.position = new Vector3(-84.88f, -0.7f, -7.24f);
where my target (nextPos) is at, but then it doesn't move but transforms but to the right position at least. Is it somehow possible to make it move to the position -84.88f, -0.7f, -7.24f?
Thank you
If there's any need I can add video on YouTube of what happens etc. Isn't there really anyway to make it move to a certain position?
//childTransform.transform.position = new Vector3(-84.88f, -0.7f, -7.24f);
Works perfectly, but would it be possible to make it move straight line and make it stop at a certain position? Thank you
just a thing... its bad practice to be using magic numbers like above. if you change where the target is then suddenly things won't work. just a little tip.