Ram object not placed at the first path

Hi, i have a question why is my ram object not placed at the beginning of the path when I drag it into the world? When I drag it, the ram is positioned in the middle of the path (2,2), and it seems not to follow the path according to the order I specified in the list. What could be the cause?

in this case i’m using unity verison 2022.3

and this is the code:

using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using UnityEngine.Animations;
using Vector3 = UnityEngine.Vector3;

public class EnemyMover : MonoBehaviour
{
    [SerializeField] List<Waypoint> path = new List<Waypoint>();
    [SerializeField] [Range(0f, 5f)] float speed = 1f;
    // Start is called before the first frame update
   void Start()
    {
        FindPath();
        ReturnToStart();
        StartCoroutine(FollowPath());
    }

    void FindPath()
    {
        path.Clear();

        GameObject[] waypoints = GameObject.FindGameObjectsWithTag("Path");

        foreach(GameObject waypoint in waypoints) 
        {
            path.Add(waypoint.GetComponent<Waypoint>());
        }
    }

    void ReturnToStart()
    {
        transform.position = path[0].transform.position;
    }

    IEnumerator FollowPath() 
    {
        foreach(Waypoint waypoint in path) 
        {
            Vector3 startPosition = transform.position;
            Vector3 endPosition = waypoint.transform.position;
            float travelPercent = 0f;

            transform.LookAt(endPosition);

            while(travelPercent < 1f) {
                travelPercent += Time.deltaTime * speed;
                transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
                yield return new WaitForEndOfFrame();
            }
        }

        Destroy(gameObject);
    }

}

What did you define the path to be? We can’t see your path, so we can’t really give an answer other than “Maybe your first path object is at x=0, z=0”. Looking at your Ram, it’s not at (2,2) it’s at (0,0)

I’m sorry for that, maybe like this


Hi Scenario_ArtZ,

Welcome to our community, and Happy New Year! :slight_smile:

The position values in your last screenshot seem to be off. What you see there is the local position relative to the parent.

image

The ‘Path’ game object is a child of ‘Environment’. Ideally, both positions are set to (0, 0, 0).

The questions is: Where is the Waypoint object used by the EnemyMover? This is relevant information because, in the EnemyMover object, we move the enemy towards waypoint.transform.position. Maybe the waypoint is not what you think it is. Bear in mind that we have multiple waypoints in the scene, and there is basically no way to distinguish between them unless we check the position of the game object to which the waypoint is assigned.

1 Like

What @Nina said. You are using FindGameObjectsWithTag("Path") to get the waypoints, but you have no idea in which order you will be getting them. You may not - and possibly don’t - get them in the order they are in the hierarchy. You have a field that is available in the inspector for the path. Just use that. Drag the waypoints into it. Then make sure the first waypoint is at the top and the order is the correct order.


Edit
I just answered another post with this issue; In the ‘Refactoring’ lecture later on, Gary mentions the fact that the FindGameObjectsWithTag will not get the waypoints in the order we expect and fixes it

1 Like

@Nina @bixarrio Thank you all for helping me with this case ❤️‍🔥

You’re welcome. Were you able to solve the problem? :slight_smile:


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms