Problem with AI movement

I used the AI movement from the pong demo. I setup waypoints. When the AI reaches the waypoint it should move to the next waypoint. The issue is that my x and y checks are not correct when I reach the waypoint. I should note that this does work if I spawn right on the correct x or y value but if I have to move along the axis it will never work.



As you can see in the image the if statement should be false. You can see the values above that both show a -2. Both objects have a TSVector2 for the x and y position. Here is the code:

using UnityEngine;
using System.Collections;
using TrueSync;

public class PlayerMovement2D : TrueSyncBehaviour {
    
    [AddTracking]
    int cur = 0;    
    public int maxX;    
    public int maxY;    
    [AddTracking]
    public float speedX;    
    [AddTracking]
    public float speedY;    
    private TSRigidBody2D tsRigidBody;
    public GameObject wayPointHold;
    private Waypoints waypoints;
    private FP xDir;
    private FP yDir;
    private FP moveX;
    private FP moveY;
    private bool changeWayPoint1;
    private bool changeWayPoint2;

    public override void OnSyncedStart()
    {
        StateTracker.AddTracking(this);

        tsRigidBody = GetComponent<TSRigidBody2D>();
        wayPointHold = GameObject.Find("Waypoints");
        waypoints = wayPointHold.GetComponent<Waypoints>();
    }

    public override void OnSyncedUpdate()
    {
        TSVector2 currentPosition = tsRigidBody.position;
        //always set move back to 0 to stop moving
        moveX = .00;
        moveY = .00;

        //get our direction and add use that speed
        
        if (currentPosition.x > waypoints.waypointsArray[cur].position.x)
        {
            moveX = -speedX;
        }
        if (currentPosition.y > waypoints.waypointsArray[cur].position.y)
        {
            moveY = -speedY;
        }
        if (currentPosition.x < waypoints.waypointsArray[cur].position.x)
        {
            moveX = speedX;
        }
        if (currentPosition.y < waypoints.waypointsArray[cur].position.y)
        {
            moveY = speedY;
        }

        
        if (!changeWayPoint1 && tsRigidBody.position.x != waypoints.waypointsArray[cur].position.x)
        {
            //    currentPosition.x = TSMath.Round((currentPosition.x * 100) / 100);
            currentPosition.x += moveX;
        }        
        else
        {
            // Waypoint reached, select next one
            changeWayPoint1 = true;            
        }
        if (!changeWayPoint2 && tsRigidBody.position.y != waypoints.waypointsArray[cur].position.y)
        {
            //    currentPosition.y = TSMath.Round((currentPosition.y * 100) / 100);
            currentPosition.y += moveY;
        }
       else
         {
            changeWayPoint2 = true;
        }
        // update our position
        tsRigidBody.position = currentPosition;
        if (changeWayPoint1 && changeWayPoint2)
        {
            cur = (cur + 1) % waypoints.waypointsArray.Length;
            changeWayPoint1 = false;
            changeWayPoint2 = false;
        }
    }
}


Answers

  • Hi @Kobaltic, it is a weird behaviour, can you Debug.Log those values, maybe they are not exatcly "-2" but an approximated value.
  • I debugged but got the same values. As you can see it is -1.99 then the AI moves -.01 and I get to -2 at which point the AI gets a .01 to move again but it shouldn't.

    kobaltic.com/debug/playerx.PNG

    I do a round on my input to control the spawn location for the AI. This way it always spawns on a 100th unit. Here is the code just in case I missed something:
    public class PlayerSpawn : TrueSyncBehaviour
    {
        public GameObject playerPrefab;    
        private TSVector playerPos;
        private Camera currentCamera;
        private Vector3 mousePos;
        private Vector3 newMousePos;  
        bool mouseButtonDown;
        FP coolTime = 0.5;
        int spawn;
        private Vector3 currMousePos;
        private FP mouseX;
        private FP mouseY;
        private FP mouseZ;
       
        public override void OnSyncedStart()
        {
          base.OnSyncedStart();
          currentCamera = Camera.main;
           
        }
    
        public override void OnSyncedInput()
        {      
            mouseButtonDown = Input.GetMouseButton(0);       
           
            if (mouseButtonDown)
            {         
                mousePos = Input.mousePosition;
                mousePos.z = 10;
                newMousePos = currentCamera.ScreenToWorldPoint(mousePos);
                mouseX = newMousePos.x;
                mouseY = newMousePos.y;
                mouseZ = newMousePos.z;
                //Round our corridents so we always spawn on a 100th of a float point
                mouseX = TSMath.Round((mouseX * 100) / 100);
                mouseY = TSMath.Round((mouseY * 100) / 100);
                playerPos = new TSVector(mouseX, mouseY,mouseZ);
                TrueSyncInput.SetTSVector(1, playerPos);           
            }
            TrueSyncInput.SetInt(0, mouseButtonDown ? 1 : 0);
        }
    
        public override void OnSyncedUpdate()
        {              
            spawn = 0;
            if (coolTime <= 0)
            {
                 spawn = TrueSyncInput.GetInt(0);
            }
        
            TSVector spawnPos = TrueSyncInput.GetTSVector(1);
            if (spawn == 1 )
            {
               TrueSyncManager.SyncedInstantiate(playerPrefab, spawnPos, TSQuaternion.identity);
                spawn = 0;
                coolTime = 0.5;
            }   
            coolTime  -= TrueSyncManager.DeltaTime;
        }
    }
  • Hi @Kobaltic, this round you did solve the problem? If not can you share some sample project by private message? I could take a better look this way.
  • Didn't work. I sent you a PM with the project. Just click on the screen to spawn.