Skip to content
Permalink
Browse files
Add files via upload
  • Loading branch information
wul25 committed Apr 30, 2021
0 parents commit ea72d3274e02fc0044d1366be7a980f748ffbdc5
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 0 deletions.
@@ -0,0 +1,123 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AIobject : MonoBehaviour
{
[HideInInspector]
//The partial velocity in the x direction
public float moveVx;
[HideInInspector]
//The partial velocity in the y direction
public float moveVy;
public float maxspeed;


//2 dimensional coordinates (x,y)

public Vector2 Position
{
get
{
return new Vector2(this.transform.position.x, this.transform.position.y);
}
}
private Vector2 _vHeading;


//Set the normalized vector m_vHeading of the forward direction of the missile

public Vector2 vHeading
{
get
{
float length = Mathf.Sqrt(moveVx * moveVx + moveVy * moveVy);
if (length != 0)
{
_vHeading.x = moveVx / length;
_vHeading.y = moveVy / length;
}
return _vHeading;
}
}
private Vector2 _vSide;

// The vertical vector of the forward direction

public Vector2 vSide
{
get
{
_vSide.x = -vHeading.y;
_vSide.y = vHeading.x;
return _vSide;
}
}


/// The velocity vector

public Vector2 Velocity
{
get
{
return new Vector2(moveVx, moveVy);
}
}

/// The speed scalar

public float Speed
{
get
{
return Mathf.Sqrt(moveVx * moveVx + moveVy * moveVy);
}
}
public float MaxSpeedRate;



/// Move the object



// The speed rate of movement, generally 1

//Do you want the velocity vector to align with the orientation of the object

public void Move(float speedRate, bool isLookAtVelocityVector)
{
this.transform.position += new Vector3(moveVx * Time.deltaTime, moveVy * Time.deltaTime, 0) * speedRate;
// Adjust the orientation of the enemy so that it is the same direction as the velocity vector
if (isLookAtVelocityVector)
{
LookAtVelocityVector();
}
}
/// <summary>
/// Keep the object always in the direction of the vector velocity
/// </summary>
void LookAtVelocityVector()
{
float zAngles = Mathf.Atan(moveVx / moveVy) * (-180 / Mathf.PI);
if (moveVy == 0)
{
zAngles = moveVx > 0 ? -90 : 90;
// Different from the previous calculation perspective, the independent judgment is added here, so that the original state can be maintained when the control is not controlled
if (moveVx == 0)
{
zAngles = this.transform.rotation.eulerAngles.z;
}
}

if (moveVy < 0)
{
zAngles = zAngles - 180;
}
Vector3 tempAngles = new Vector3(0, 0, zAngles);
Quaternion tempQua = this.transform.rotation;
tempQua.eulerAngles = tempAngles;
this.transform.rotation = tempQua;
}
}
@@ -0,0 +1,78 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class aiseek : MonoBehaviour
{

public AIobject m_pursuiter;
public AIobject m_pursuitTarget;
public Image AimPredictionPos;

void Start()
{
}


void Update()
{

Vector2 moveVec = AI_PredictionPursuit();
m_pursuiter.moveVx += moveVec.x;
m_pursuiter.moveVy += moveVec.y;
m_pursuiter.Move(1, true);
}

/// The interception and chase algorithm can predict the moving position of the target for tracking

Vector2 AI_PredictionPursuit()
{
// First calculate the distance between the two
Vector2 ToPursuit = m_pursuitTarget.Position - m_pursuiter.Position;
// The dot product of the forward direction vector of the local coordinates
float RelativeHeading = DotProduct(m_pursuiter.vHeading, m_pursuitTarget.vHeading);
// If the distance between the two is greater than zero in the direction of the local vector of the pursuer, then the pursuer should move directly toward the object being tracked, where 20 is the opposite direction of the object being tracked and the pursuer's orientation is considered to be facing at 18 degrees (cos(18)=0.95)

if (DotProduct(ToPursuit, m_pursuiter.vHeading) > 0 && RelativeHeading < -0.95f)
{
// Debug.Log("relativeHeading:" + RelativeHeading);
return AI_Seek(m_pursuitTarget.Position);
}
// The predicted time is proportional to the distance between the tracked and the tracked, and inversely proportional to the speed of the tracked and the current predicted position close to the tracked.
float toPursuitLenght = Mathf.Sqrt(ToPursuit.x * ToPursuit.x + ToPursuit.y * ToPursuit.y);
float LookAheadTime = toPursuitLenght / (m_pursuiter.maxspeed + m_pursuitTarget.Speed);
// The predicted location will always be in front of the local coordinates of the object being tracked
Vector2 predictionPos = m_pursuitTarget.Position + m_pursuitTarget.Velocity * LookAheadTime;
Debug.Log("preditionx:" + predictionPos.x + "preditiony:" + predictionPos.y);
AimPredictionPos.transform.position = new Vector3(predictionPos.x, predictionPos.y, 0);
return AI_Seek(predictionPos);
}
/// <summary>
/// Arrive at the specified location
/// </summary>


Vector2 AI_Seek(Vector2 TargetPos)
{
// Calculate the vector between the target position and the tracking object position and normalize it
Vector2 DesiredVelocity = (TargetPos - m_pursuiter.Position).normalized * m_pursuiter.maxspeed;
// You can create an intermediate transition vector by subtracting it directly, avoiding the need to change it directly
DesiredVelocity = DesiredVelocity - m_pursuiter.Velocity;
return DesiredVelocity;
}
/// <summary>
/// Calculate the dot product of matrix A with matrix B

/// <param name="A"></param>
/// <param name="B"></param>

float DotProduct(Vector2 A, Vector2 B)
{
return A.x * B.x + A.y * B.y;
}




}

0 comments on commit ea72d32

Please sign in to comment.