[ad_1]
I’ve been attempting to implement this steering habits in Unity: Understanding Steering Behaviors: Collision Avoidance
However, I’m getting unusual outcomes. Here’s the code for the script, be happy to counsel higher methods to code some issues..
#pragma strict
#pragma downcast
public var take a look at: boolean;
personal var cease:boolean;
personal var vel: Vector3; //Velocity
personal var qa: Vector3;
personal var forward: Vector3; //Ahead vector - A degree in entrance of this unit
personal var ahead2: Vector3; // Shorter Ahead Vector - Used for detecting nearer objects
personal var spwnScript : EnemySpawner_2; //Reference to script of Spawner
personal var differentColliderLst: List.<SphereCollider>; //Should holds reference to variable of different script's (spawner) collider record.
personal var differentCollider : SphereCollider; // Used to retailer particular person colliders from record
personal var personalCollider : SphereCollider;
personal var closestCollider : SphereCollider;
personal var spwnObj : GameObject;
//Steering Behavior Variables
personal var steering : Vector3; //Variable used so as to add to the speed, and to produce other behaviors assigned to it.
personal var avoidance_frc : Vector3; //Avoidance Force
personal var isCollision : boolean;
//CONSTANTS
personal var MAX_QUEUE_ahead: float;
personal var MAX_AVOID_FRC: float;
personal var pace:float = 70;
personal var injury:int = 25;
perform Awake()
{
//Destroy(gameObject, 3);
MAX_AVOID_FRC = 2f;
MAX_QUEUE_ahead = 30f;
cease = false;
steering = Vector3(0f,0f,0f);
differentCollider = null;
personalCollider = gameObject.GetComponent("SphereCollider") as SphereCollider;
spwnObj = GameObject.Find("EnemySpawn_2").gameObject;
spwnScript = spwnObj.GetComponent(EnemySpawner_2);
differentColliderLst = spwnScript.colliderList;
}
perform Start ()
{
//GameObject.Find("EnemySpawner_2").remodel.GetComponent("EnemySpawner_2");
yield WaitForSeconds(Random.Range(1f, 1.3f));
cease = true;
}
perform Update () {
if(!cease){
vel = Vector3(0,0,-speed * Time.deltaTime);
steering += collisionAvoidance();
Debug.Log(steering);
vel += steering;
remodel.Translate(vel);
}
}
perform OnCollisionEnter(col:Collision)
{
if(col.gameObject.tag == "Player")
{
yield WaitForSeconds(.1); //So that bullets appears to have an impact after they contact part of the ship, as an alternative of an invisible field
NotificationCenter.DefaultCenter().PostNotification(this, "PlayerHit",injury);
//playerScript.ReceiveDamage(injury);
Destroy(gameObject);
}
}
// Functions utilized by Steering Behaviors //
/////////////////////////////////////////
personal perform collisionAvoidance() : Vector3 {
qa = vel.normalized * MAX_QUEUE_ahead;
forward = qa + remodel.place;
ahead2 = qa * 0.5f + remodel.place;
differentCollider = findNextClosestSphere() ;
avoidance_frc = Vector3(0f,0f,0f);
if(differentCollider != null) {
avoidance_frc.x = forward.x - differentCollider.remodel.place.x;
avoidance_frc.y = forward.y - differentCollider.remodel.place.y;
avoidance_frc.z = forward.z - differentCollider.remodel.place.z;
Debug.Log("AVOID"+avoidance_frc);
avoidance_frc.Normalize();
avoidance_frc.Scale(Vector3(MAX_AVOID_FRC, MAX_AVOID_FRC, MAX_AVOID_FRC));
}
else{
avoidance_frc.Scale(Vector3(0f, 0f, 0f)); // nullifty the avoidance drive
}
return avoidance_frc;
}
personal perform findNextClosestSphere() : SphereCollider {
closestCollider = null;
for(var i:int = 0; i < differentColliderLst.Count; i++)
{
if(differentColliderLst[i].remodel.place != personalCollider.remodel.place)
{
differentCollider = differentColliderLst[i];
isCollision = lineIntersectsSphere(forward, ahead2, differentCollider);
if(isCollision && (closestCollider == null || distance(gameObject.remodel.place, differentCollider.remodel.place) < distance(gameObject.remodel.place, closestCollider.remodel.place))) {
closestCollider = differentCollider;
}
}
}
//Set Sphere Collider
//For readability functions, the perform returns again a vector.
//An different, could be for the perform to easily set the worth of the oppositeCollider variable
//and never return something.
//The variable sits exterior any perform, and can be utilized anyplace within the script.
return closestCollider;
}
personal perform distance(a:Vector3, b:Vector3){
return Mathf.Sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z) * (a.z - b.z));
}
personal perform lineIntersectsSphere(ahd:Vector3, ahd2:Vector3, sphCol:SphereCollider) : boolean
I’ve one other script titled EnemySpawner_2, which is hooked up to a gameobject within the scene, and is answerable for spawning and saving spheres to an inventory, which could be accessed by the above script with the differentColliderLst variable.
Now the problem is that the enemies that spawn, do NOT really do any collision avoidance in any respect. Rather, they spawn one after the other, from the identical space, and so they rush in the identical course (on one axis) in direction of the closest enemy that’s detected. I’m attempting to know why this habits is occurring. I noticed that a part of the issue is that not all axes have values within the avoidance_frc variable on the finish collisionAvoidance() so the drive is just utilized to 1 axis, when it must be carried out in two, which is the z AND the x axis (The axis that presently incorporates no worth).
But I do not perceive how the code doesn’t run into these points within the different tutorial. I’ve principally carried out a Unity copy of the code and have been debugging for this for hours.. what have I carried out flawed?
[ad_2]