Quantcast
Channel: Recent Discussions — Photon Engine
Viewing all articles
Browse latest Browse all 15755

Problem with AI movement

$
0
0
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;
}
}
}


Viewing all articles
Browse latest Browse all 15755

Trending Articles