Zombies + Skeleton Physics arrow + aimbot
360 headhsot ezz
This commit is contained in:
parent
b3a87fb2a6
commit
bf010f3476
91 changed files with 28021 additions and 84 deletions
|
@ -8,13 +8,19 @@ public class ArrowHandler : MonoBehaviour
|
|||
{
|
||||
[SerializeField] private float baseDamage;
|
||||
[SerializeField] private float baseKnockback;
|
||||
public Rigidbody _rigidBody;
|
||||
private Rigidbody _rigidBody;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_rigidBody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Align with speed
|
||||
if (_rigidBody.linearVelocity.magnitude >= 1f) transform.forward = _rigidBody.linearVelocity.normalized;
|
||||
}
|
||||
|
||||
public void LaunchArrow(Vector3 baseSpeed)
|
||||
{
|
||||
_rigidBody.linearVelocity = baseSpeed;
|
||||
|
@ -33,5 +39,7 @@ public class ArrowHandler : MonoBehaviour
|
|||
}
|
||||
unit.TakeDamage(baseDamage);
|
||||
}
|
||||
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ public class AttackHandler : MonoBehaviour
|
|||
[SerializeField] protected float knockbackVerticalForce;
|
||||
|
||||
protected float _timer;
|
||||
private MinecraftUnit _minecraftUnit;
|
||||
protected MinecraftUnit _minecraftUnit;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
|
|
|
@ -5,14 +5,55 @@ public class AttackSkeleton : AttackHandler
|
|||
[SerializeField] private GameObject arrowPrefab;
|
||||
[SerializeField] private float arrowBaseSpeed;
|
||||
[SerializeField] private Transform spawnPos;
|
||||
[SerializeField] private bool directShot;
|
||||
|
||||
|
||||
public override bool Attack()
|
||||
{
|
||||
if (_timer > 0) return false;
|
||||
float launchAngle = findLaunchAngle();
|
||||
//print(launchAngle);
|
||||
// If target not reachable
|
||||
if (launchAngle < 0) return false;
|
||||
_timer = cooldown;
|
||||
|
||||
GameObject arrow = Instantiate(arrowPrefab, spawnPos.position, spawnPos.rotation);
|
||||
ArrowHandler arrowHandler = arrow.GetComponent<ArrowHandler>();
|
||||
arrowHandler.LaunchArrow(arrowBaseSpeed * spawnPos.forward);
|
||||
// In target <-> launcher + transform.up basis
|
||||
Vector2 localLaunchVector = arrowBaseSpeed * new Vector2(Mathf.Cos(launchAngle), Mathf.Sin(launchAngle));
|
||||
// Transform it in global basis
|
||||
AbstractUnit targetUnit = _minecraftUnit.MovementHandler.TargetUnit;
|
||||
Vector3 diffVector = Vector3.ProjectOnPlane(targetUnit.transform.position - spawnPos.position, Vector3.up);
|
||||
|
||||
Vector3 launchVectorNormalized = (localLaunchVector.x * diffVector.normalized + localLaunchVector.y * Vector3.up).normalized;
|
||||
arrowHandler.LaunchArrow(launchVectorNormalized * arrowBaseSpeed);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private float findLaunchAngle()
|
||||
{
|
||||
// Source : https://en.wikipedia.org/wiki/Projectile_motion#Angle_%CE%B8_required_to_hit_coordinate_(x,_y)
|
||||
|
||||
AbstractUnit targetUnit = _minecraftUnit.MovementHandler.TargetUnit;
|
||||
Vector3 diffVector = Vector3.ProjectOnPlane(targetUnit.transform.position - spawnPos.position, Vector3.up);
|
||||
|
||||
float x = Vector3.ProjectOnPlane(diffVector, Vector3.up).magnitude;
|
||||
float y = diffVector.y;
|
||||
float g = Physics.gravity.magnitude;
|
||||
float v = arrowBaseSpeed;
|
||||
float v_sqr = v * v;
|
||||
//print("x : " + x);
|
||||
|
||||
float inside_sqrt_root = v_sqr*v_sqr - g*(g*x*x + 2*y*v_sqr);
|
||||
if (inside_sqrt_root < 0.0f) return -1f; // Can't reach target
|
||||
|
||||
// directShot is the smallest angle, undirectShot shot is the biggest angle
|
||||
float numerator = directShot ? v_sqr - Mathf.Sqrt(inside_sqrt_root) : v_sqr + Mathf.Sqrt(inside_sqrt_root);
|
||||
float inside_arctan = numerator / (g * x);
|
||||
print(inside_arctan);
|
||||
return Mathf.Atan(inside_arctan);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
@ -8,12 +9,18 @@ using UnityEngine.AI;
|
|||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class MovementHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float speed;
|
||||
[SerializeField] public float speed;
|
||||
[SerializeField] private NavMeshAgent agent;
|
||||
[SerializeField] private Transform defaultMoveTarget;
|
||||
|
||||
private float knockbackTime = 1.2f;
|
||||
private float noNavMeshDeadTime = 6.0f;
|
||||
|
||||
[HideInInspector] public AbstractUnit TargetUnit {get; private set; }
|
||||
|
||||
private MinecraftUnit _minecraftUnit;
|
||||
private Rigidbody _rigidbody;
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
|
@ -21,6 +28,16 @@ public class MovementHandler : MonoBehaviour
|
|||
_rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
agent.speed = speed;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Mathf.Abs(agent.speed - speed) < 0.01f) agent.speed = speed;
|
||||
}
|
||||
|
||||
public void ChangeSpeed(float newSpeed)
|
||||
{
|
||||
speed = newSpeed;
|
||||
|
@ -43,7 +60,8 @@ public class MovementHandler : MonoBehaviour
|
|||
|
||||
public void MoveTowardsNearest()
|
||||
{
|
||||
MoveTowards(FindNearestEnemy().transform.position);
|
||||
TargetUnit = FindNearestEnemy();
|
||||
MoveTowards(TargetUnit.transform.position);
|
||||
}
|
||||
|
||||
AbstractUnit FindNearestEnemy()
|
||||
|
@ -70,7 +88,7 @@ public class MovementHandler : MonoBehaviour
|
|||
// Unity navmesh, can't handle physics (it rewrite velocity). So we deactivate it when applying force.
|
||||
agent.enabled = false;
|
||||
_rigidbody.AddForce(impulse, ForceMode.Impulse);
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
yield return new WaitForSeconds(knockbackTime);
|
||||
|
||||
float noSurfaceTime = 0.0f;
|
||||
|
||||
|
@ -81,7 +99,7 @@ public class MovementHandler : MonoBehaviour
|
|||
noSurfaceTime += 0.5f;
|
||||
|
||||
// Die if exited navMesh for to long
|
||||
if (noSurfaceTime > 6.0f)
|
||||
if (noSurfaceTime > noNavMeshDeadTime)
|
||||
{
|
||||
_minecraftUnit.HealthHandler.Death();
|
||||
yield break;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue