public override Vector3 CaculateSteeringForce(float dt, BehavorContext behavorContext) { m_PositionTarget = m_Target.position; //Declaring the m_PositionTarget; float _distance = Vector3.Distance(behavorContext.Position, m_Target.position); Vector3 dir = Vector3.zero; float neededDistance = m_Distance; if (neededDistance == 0) { m_Distance = behavorContext.Settings.m_FleeDistance; } if (_distance <= neededDistance) { dir = m_PositionTarget - behavorContext.Position; //Making a local vector3 to Make it more readable; } else { return(-behavorContext.Velocity); } m_VelocityDesired = -dir * behavorContext.Settings.m_MaxVelocityDesired; //Setting the dir to nagivate to Go the other way; return(m_VelocityDesired - behavorContext.Velocity); //Returen the velocity }
private Vector3 GetAllDir(BehavorContext context) { // We stoppen in een allen colliders in een array door een overlapsphere Collider[] Colliders = Physics.OverlapSphere(context.Position, Radius * 1.2f, LayerMask); // We Vector3 Dir = Vector3.zero; foreach (Collider collider in Colliders) { Vector3 directiontoCol = collider.transform.position - context.Position; if (Physics.Raycast(context.Position, directiontoCol, out RaycastHit hit, LayerMask)) { if (context.Settings.m_AvoidanceType == SteeringSettings.AvoidanceType.Ricochet) { Vector3 Closetpoint = hit.point; m_LastHitpos = Closetpoint; Dir += (((Closetpoint - collider.transform.position)) * GetMultiPlyer(Vector3.Distance(context.Position, Closetpoint))) * Radius; } else { Vector3 Hitpos = hit.point; m_LastHitpos = Hitpos; Dir += (((context.Position - Hitpos)) * GetMultiPlyer(Vector3.Distance(context.Position, Hitpos))) * Radius; } m_TargetPos = Dir + m_LastHitpos; } } return(Dir); }
public override void OnDrawGizmos(BehavorContext behavorContext) { base.OnDrawGizmos(behavorContext); Support.DrawLine(behavorContext.Position, behavorContext.Position + behavorContext.Velocity, Color.red); Support.Point(m_PositionTarget, 0.5f, Color.red); Support.DrawLabel(behavorContext.Position + behavorContext.Velocity, Label, Color.red); }
public override Vector3 CaculateSteeringForce(float dt, BehavorContext behavorContext) { Vector2 RandomDir = RandomDir = new Vector2(Random.Range(-10f, 10f), Random.Range(-10f, 10f)); if (behavorContext.Position.x < EnitiyManager.instance.m_MinPos.x || behavorContext.Position.y < EnitiyManager.instance.m_MinPos.y) { RandomDir = Vector3.zero - behavorContext.Position; RandomDir = RandomDir.normalized * 10; } if (behavorContext.Position.x > EnitiyManager.instance.m_MaxPos.x || behavorContext.Position.y > EnitiyManager.instance.m_MaxPos.y) { RandomDir = Vector3.zero - behavorContext.Position; RandomDir = RandomDir.normalized * 10; } Vector2 pos = behavorContext.Position; m_PositionTarget = pos + (m_OldDirection + RandomDir * 2f); Vector3 dir = m_PositionTarget - behavorContext.Position; m_OldDirection = RandomDir; m_VelocityDesired = dir * behavorContext.Settings.m_MaxVelocityDesired; return(m_VelocityDesired - behavorContext.Velocity); }
public override void OnDrawGizmos(BehavorContext behavorContext) { base.OnDrawGizmos(behavorContext); foreach (Vector3 hidespot in m_HideSpots) { Support.DrawWiredSphere(hidespot, 1, Color.blue); } }
public override void OnDrawGizmos(BehavorContext behavorContext) { base.OnDrawGizmos(behavorContext); for (int i = 1; i < m_WaitPoints.Count; i++) { Support.DrawLine(m_WaitPoints[i - 1].position, m_WaitPoints[i].position, Color.blue); } }
public override void OnDrawGizmos(BehavorContext behavorContext) { base.OnDrawGizmos(behavorContext); Support.DrawCircle(m_CenterCirlce, behavorContext.Settings.m_WanderCircleRadius, Color.green); Support.DrawLine(m_CenterCirlce, m_PositionTarget, Color.blue); Support.DrawLine(behavorContext.Position, m_CenterCirlce, Color.green); Support.DrawLabel(m_PositionTarget, "Target", Color.blue); }
public override void OnDrawGizmos(BehavorContext behavorContext) { base.OnDrawGizmos(behavorContext); Vector3 posvelocity = behavorContext.Position + behavorContext.Velocity; Support.DrawLine(behavorContext.Position, posvelocity, Color.red); Support.Point(posvelocity, 0.5f, Color.red); }
private Vector3 GetPositionDir(BehavorContext context) { Vector3 WaitPoint1 = m_WaitPoints[WaitPointIndex - 1].position; Vector3 WaitPoint2 = m_WaitPoints[WaitPointIndex].position; Vector3 pos = FindNearestPointOnLine(WaitPoint1, WaitPoint2, context.Position); Debug.DrawLine(context.Position, pos); return(pos); }
private Vector3 GetClosestPoint(BehavorContext context) { m_HideSpots = GetHideSpots(context); float[] values = new float[m_HideSpots.Length]; for (int i = 0; i < m_HideSpots.Length; i++) { values[i] = Vector3.Distance(m_Target.position, m_HideSpots[i]); } return(m_HideSpots[GetIndexFromFar(values)]); }
private Vector3[] GetHideSpots(BehavorContext context) { Vector3[] hidespots = new Vector3[m_HideColliders.Length]; for (int i = 0; i < m_HideColliders.Length; i++) { Vector3 dir = m_HideColliders[i].transform.position - m_Target.position; Vector3 closestpoint = m_HideColliders[i].ClosestPoint(m_HideColliders[i].transform.position + dir); hidespots[i] = closestpoint + (dir.normalized * context.Settings.m_HideOffset); } return(hidespots); }
private float GetPriorty(float distance, BehavorContext context) { float ammount = 0; if (distance <= Radius * 0.5f) { ammount = context.Settings.m_MaxPriorty; return(ammount); } ammount = context.Settings.m_MinPriorty; return(ammount); }
public override Vector3 CaculateSteeringForce(float dt, BehavorContext behavorContext) { if (m_HideColliders.Length > 0) { m_PositionTarget = GetClosestPoint(behavorContext); m_VelocityDesired = (m_PositionTarget - behavorContext.Position) * behavorContext.Settings.m_MaxVelocityDesired; return(m_VelocityDesired - behavorContext.Velocity); } else { return(Vector3.zero); } }
public override Vector3 CaculateSteeringForce(float dt, BehavorContext behavorContext) { Vector3 TargetsDir = m_Target.position - m_Oldpos; Vector3 futurePos = m_Target.position + TargetsDir.normalized * behavorContext.Settings.m_MaxSpeed; m_PositionTarget = futurePos; Vector3 dir = m_PositionTarget - behavorContext.Position; m_Oldpos = m_Target.position; m_VelocityDesired = dir * behavorContext.Settings.m_MaxVelocityDesired; return(m_VelocityDesired - behavorContext.Velocity); }
public override Vector3 CaculateSteeringForce(float dt, BehavorContext behavorContext) { float _distance = Vector3.Distance(behavorContext.Position, m_Target.position); Vector3 dir = Vector3.zero; if (_distance > behavorContext.Settings.m_StopDistance) { m_PositionTarget = m_Target.position; dir = m_PositionTarget - behavorContext.Position; } m_VelocityDesired = dir * behavorContext.Settings.m_MaxVelocityDesired; return(m_VelocityDesired - behavorContext.Velocity); }
public override void OnDrawGizmos(BehavorContext behavorContext) { base.OnDrawGizmos(behavorContext); Color color = Color.blue; Support.DrawWiredSphere(behavorContext.Position, Radius, color); if (Priorty != 0) { color.a = 50f; Support.DrawLine(behavorContext.Position, behavorContext.Position + behavorContext.Velocity, Color.white); Support.DrawWiredSphere((m_PositionTarget - behavorContext.Position).normalized + behavorContext.Position, 1f, Color.red); DrawAvoid(behavorContext); } }
public override Vector3 CaculateSteeringForce(float dt, BehavorContext behavorContext) { m_WanderAngle += Random.Range(-5.0f * behavorContext.Settings.m_WanderNoiseAngle * Mathf.Deg2Rad, 5.0f * behavorContext.Settings.m_WanderNoiseAngle * Mathf.Deg2Rad); m_CenterCirlce = behavorContext.Position + behavorContext.Velocity.normalized * behavorContext.Settings.m_WanderCircleDistance; Vector3 offset = new Vector3(behavorContext.Settings.m_WanderCircleRadius * Mathf.Cos(m_WanderAngle), 0.0f, behavorContext.Settings.m_WanderCircleRadius * Mathf.Sin(m_WanderAngle)); m_PositionTarget = m_CenterCirlce + offset; Vector3 dir = m_PositionTarget - behavorContext.Position; m_VelocityDesired = dir * behavorContext.Settings.m_MaxVelocityDesired; return(m_VelocityDesired - behavorContext.Velocity); }
public override Vector3 CaculateSteeringForce(float dt, BehavorContext behavorContext) { Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); if (input != Vector3.zero) { m_PositionTarget = behavorContext.Position + input.normalized * behavorContext.Settings.m_MaxVelocityDesired; } else { m_PositionTarget = behavorContext.Position; } m_VelocityDesired = (m_PositionTarget - behavorContext.Position) * behavorContext.Settings.m_MaxVelocityDesired; return(m_VelocityDesired - behavorContext.Velocity); }
public override Vector3 CaculateSteeringForce(float dt, BehavorContext behavorContext) { m_PositionTarget = m_Target.position; Vector3 stopVector = (behavorContext.Position - m_PositionTarget).normalized * behavorContext.Settings.m_ArriveDistance; Vector3 stopPostition = m_PositionTarget + stopVector; TargetOffset = stopPostition - behavorContext.Position; Distance = TargetOffset.magnitude; DoBrake = behavorContext.Settings.m_SlowingDistance >= Vector3.Distance(behavorContext.Position, m_PositionTarget); if (DoBrake) { Debug.Log("Breaking"); } RampedSpeed = behavorContext.Settings.m_MaxVelocityDesired * (Distance / behavorContext.Settings.m_SlowingDistance); ClippedSpeed = Mathf.Min(RampedSpeed, behavorContext.Settings.m_MaxVelocityDesired); m_VelocityDesired = (ClippedSpeed / Distance) * TargetOffset; return((m_VelocityDesired - behavorContext.Velocity) * behavorContext.Settings.m_MaxVelocityDesired); }
private Vector3 StayWithPath(BehavorContext context) { if (WaitPointIndex == 0) { return(Vector3.zero); } Vector3 pos = GetPositionDir(context); float _Dis = Vector3.Distance(pos, context.Position); if (_Dis < context.Settings.m_PathDistance) { return(Vector3.zero); } float Mulitplyer = GetMultiPlyer(_Dis); Vector3 dir = pos - context.Position; return(dir * Mulitplyer); }
private Vector3 GetNextPoint(BehavorContext Context) { float Distance = Vector3.Distance(Context.Position, m_WaitPoints[WaitPointIndex].position); if (Distance <= Context.Settings.m_PointArriveDistance) { if (WaitPointIndex < m_WaitPoints.Count - 1) { WaitPointIndex++; } else { Arrived = true; OnArrived.Invoke(); return(Context.Position); } } return(m_WaitPoints[WaitPointIndex].position); }
public override Vector3 CaculateSteeringForce(float dt, BehavorContext behavorContext) { if (Input.GetMouseButtonDown(0)) { if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, 100f)) { m_PositionTarget = hit.point; } } Vector3 dir = m_PositionTarget - behavorContext.Position; float _Dis = Vector3.Distance(behavorContext.Position, m_PositionTarget); if (_Dis < behavorContext.Settings.m_StopDistance) { dir = behavorContext.Velocity / 2f; } m_VelocityDesired = dir * behavorContext.Settings.m_MaxVelocityDesired; return(m_VelocityDesired - behavorContext.Velocity); }
public float CaculatePriorty(BehavorContext context) { float priorty = 0.0f; Collider[] Colliders = Physics.OverlapSphere(context.Position, Radius * 1.2f, LayerMask); if (Colliders.Length > 0) { foreach (Collider col in Colliders) { Vector3 dir = col.transform.position - context.Position; if (Physics.Raycast(context.Position, dir, out RaycastHit hit)) { priorty = GetPriorty(Vector3.Distance(context.Position, hit.point), context); Priorty = priorty; return(priorty); } } } Priorty = priorty; return(priorty); }
public override Vector3 CaculateSteeringForce(float dt, BehavorContext behavorContext) { if (Priorty == 0) { return(Vector3.zero); } Vector3 dir = GetAllDir(behavorContext); m_PositionTarget = dir + behavorContext.Position; float angle = Vector3.Angle(m_VelocityDesired, behavorContext.Velocity); if (angle >= 0 && angle < 1 && m_PositionTarget != Vector3.zero) { m_PositionTarget += (Vector3.Cross(behavorContext.Position + Vector3.up, behavorContext.Velocity) * behavorContext.Settings.m_MaxVelocityDesired) * (Radius * 10); } Debug.DrawLine(behavorContext.Position, behavorContext.Position + dir); m_VelocityDesired = (m_PositionTarget - behavorContext.Position) * behavorContext.Settings.m_MaxVelocityDesired; return(m_VelocityDesired - behavorContext.Velocity); }
public override Vector3 CaculateSteeringForce(float dt, BehavorContext behavorContext) { Vector3 dir = Vector3.zero; //Making a local vector3 to Make it more readable; m_PositionTarget = GetNextPoint(behavorContext); //Declaring the m_PositionTarget; if (!Arrived) { dir = m_PositionTarget - behavorContext.Position; //Setting the direction; Vector3 toLineDir = StayWithPath(behavorContext); if (toLineDir != Vector3.zero) { dir += toLineDir; } } else { m_PositionTarget = behavorContext.Position; } m_VelocityDesired = dir * behavorContext.Settings.m_MaxVelocityDesired; return(m_VelocityDesired - behavorContext.Velocity); }
public override void OnDrawGizmos(BehavorContext behavorContext) { base.OnDrawGizmos(behavorContext); Support.DrawLine(behavorContext.Position, m_PositionTarget, Color.red); Support.DrawWiredSphere(m_PositionTarget, 1f, Color.red); }
public override void OnDrawGizmos(BehavorContext behavorContext) { base.OnDrawGizmos(behavorContext); Support.DrawCircle(m_PositionTarget, behavorContext.Settings.m_ArriveDistance, Color.red); }
public override void OnDrawGizmos(BehavorContext behavorContext) { base.OnDrawGizmos(behavorContext); }
private void DrawAvoid(BehavorContext Context) { Support.DrawLine(Context.Position, m_LastHitpos, Color.blue); Support.DrawLine(m_LastHitpos, m_TargetPos, Color.green); }
public override void Start(BehavorContext behavorContext) { base.Start(behavorContext); m_HideColliders = Physics.OverlapSphere(behavorContext.Position, behavorContext.Settings.m_HideSearchRadius, behavorContext.Settings.m_HideLayerMask); }