Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbianchi committed Feb 8, 2022
0 parents commit e735d70
Show file tree
Hide file tree
Showing 100 changed files with 7,228 additions and 0 deletions.
625 changes: 625 additions & 0 deletions B2 Scripts/PrismManager.cs

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions B2 Scripts/PrismManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions B2 Scripts/Prisms.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions B2 Scripts/Prisms/IrregularPrism.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class IrregularPrism : Prism
{

void Start()
{
pointCount = Mathf.Max(pointCount, 3);
points = Enumerable.Range(0, pointCount).Select(i => Random.value).OrderBy(i => i).Select(theta => Quaternion.AngleAxis(360f * theta, Vector3.up) * Vector3.forward * 0.5f).ToArray();
points = points.Select(p => transform.position + Quaternion.AngleAxis(transform.eulerAngles.y, Vector3.up) * new Vector3(p.x * transform.localScale.x, 0, p.z * transform.localScale.z)).ToArray();
midY = 0;
height = 2;
}

}
11 changes: 11 additions & 0 deletions B2 Scripts/Prisms/IrregularPrism.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions B2 Scripts/Prisms/Prism.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Prism : MonoBehaviour
{
public int pointCount = 3;
public Vector3[] points;
public float midY, height;

public GameObject prismObject;
}
11 changes: 11 additions & 0 deletions B2 Scripts/Prisms/Prism.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions B2 Scripts/Prisms/RectPrism.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class RectPrism : Prism
{

void Start()
{
points = new Vector3[] { new Vector3(0.5f, 0, 0.5f), new Vector3(0.5f, 0, -0.5f), new Vector3(-0.5f, 0, -0.5f), new Vector3(-0.5f, 0, 0.5f) };
midY = 0;
height = 1;
}

void Update()
{
var yMin = midY - height / 2 * transform.localScale.y;
var yMax = midY + height / 2 * transform.localScale.y;
foreach (var pt in points.Select(p => new Vector3(p.x * transform.localScale.x, 0, p.z * transform.localScale.z)))
{
var point = transform.position + Quaternion.AngleAxis(transform.eulerAngles.y, Vector3.up) * pt;
Debug.DrawLine(point + Vector3.up * yMin, point + Vector3.up * yMax, Color.red);
}
}
}
11 changes: 11 additions & 0 deletions B2 Scripts/Prisms/RectPrism.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions B2 Scripts/Prisms/RegularPrism.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class RegularPrism : Prism
{
public bool forwardAxisPerpToFace = true;

void Start()
{
pointCount = Mathf.Max(pointCount, 3);
points = Enumerable.Range(0, pointCount).Select(i => Quaternion.AngleAxis(360f / pointCount * (i + (forwardAxisPerpToFace ? 0.5f : 0)), Vector3.up) * Vector3.forward * 0.5f).ToArray();
points = points.Select(p => transform.position + Quaternion.AngleAxis(transform.eulerAngles.y, Vector3.up) * new Vector3(p.x * transform.localScale.x, 0, p.z * transform.localScale.z)).ToArray();
midY = 0;
height = 2;
}

}
11 changes: 11 additions & 0 deletions B2 Scripts/Prisms/RegularPrism.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions B2 Scripts/Prisms/TriPrism.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class TriPrism : Prism
{

void Start()
{
points = new Vector3[] { new Vector3(9.2f / 2, 0, 0), new Vector3(-9.2f / 2, 0, 0), new Vector3(0, 0, 7.98f) };
midY = 0;
height = 9.709259f;
}

void Update()
{
var yMin = midY - height / 2 * transform.localScale.y;
var yMax = midY + height / 2 * transform.localScale.y;
foreach (var pt in points.Select(p => new Vector3(p.x * transform.localScale.x, 0, p.z * transform.localScale.z)))
{
var point = transform.position + Quaternion.AngleAxis(transform.eulerAngles.y, Vector3.up) * pt;
Debug.DrawLine(point + Vector3.up * yMin, point + Vector3.up * yMax, Color.red);
}
}
}
11 changes: 11 additions & 0 deletions B2 Scripts/Prisms/TriPrism.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

182 changes: 182 additions & 0 deletions B3 Scripts/AgentController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AgentController : MonoBehaviour
{
Animator anim;
NavMeshAgent agent;
NavMeshObstacle agentObstacle;
public AgentManager manager;
bool IsRotating = false;
bool IsJumping = false;
bool ActivateAgent = false;
Vector3 newTargetPosition = Vector3.zero;
Quaternion lookRotation;
bool sprint;
float rot, speed;
float baseSpeed = 1.550f;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
agentObstacle = GetComponent<NavMeshObstacle>();
agentObstacle.enabled = false;
// Don’t update position automatically
agent.updatePosition = false;
agent.autoTraverseOffMeshLink = false;
speed = baseSpeed;
}

Vector2 smoothDeltaPosition = Vector2.zero;
Vector2 velocity = Vector2.zero;


void Update()
{
if(ActivateAgent){
NavMeshPath path = new NavMeshPath();
NavMesh.CalculatePath(transform.position, transform.position, NavMesh.AllAreas, path);
if(path.corners.Length > 0){
ActivateAgent = false;
agent.enabled = true;
agent.avoidancePriority = 50;
agent.SetDestination(newTargetPosition);

agent.isStopped = true;
IsRotating = true;
agent.updatePosition = false;
agent.autoTraverseOffMeshLink = false;
}

}else if(agent.isOnOffMeshLink && !IsJumping){
IsJumping = true;
anim.SetBool("leap",true);
}else if(IsJumping){
OffMeshLinkData data = agent.currentOffMeshLinkData;

Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;

transform.position = Vector3.MoveTowards(transform.position, endPos, 2.0f * Time.deltaTime);

if(transform.position == endPos)
{
agent.CompleteOffMeshLink();
IsJumping = false;
anim.SetBool("leap", false);
}
}else if(!IsRotating && agent.enabled){
MoveAnimation();
}else if(agent.enabled){
Vector3 direction = (agent.steeringTarget - transform.position).normalized;
lookRotation = Quaternion.LookRotation(direction);

Quaternion oldRotation = transform.rotation;
transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, Time.deltaTime * 100.0f);
float angle = Quaternion.Angle( oldRotation, transform.rotation);

anim.SetFloat("sides", angle);
if (Quaternion.Angle(transform.rotation, lookRotation) < 2.5f)
{
IsRotating = false;
agent.isStopped = false;
}
}
}

void MoveAnimation(){
Vector3 worldDeltaPosition = agent.destination - transform.position;

// Map 'worldDeltaPosition' to local space
float dx = Vector3.Dot(transform.right, worldDeltaPosition);
float dy = Vector3.Dot(transform.forward, worldDeltaPosition);
Vector2 deltaPosition = new Vector2(dx, dy);

// Low-pass filter the deltaMove
float smooth = Mathf.Min(1.0f, Time.deltaTime / 0.15f);
smoothDeltaPosition = Vector2.Lerp(smoothDeltaPosition, deltaPosition, smooth);

// Update velocity if time advances
if (Time.deltaTime > 1e-5f)
velocity = smoothDeltaPosition / Time.deltaTime;

if(velocity.magnitude > 0.5f && agent.remainingDistance > agent.radius -2){
rot = Mathf.Deg2Rad * Vector2.Angle(transform.position,deltaPosition);
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Forwards")) rot = 0.0f;
// Update animation parameters
anim.SetFloat("sides", rot);
anim.SetFloat("speed", speed);

agent.speed = speed;
}else{
anim.SetFloat("sides", 0f);
anim.SetFloat("speed", 0f);
}

if (agent.enabled)
{
if (PathFindingComplete())
{
anim.CrossFade("idle", 1f);
anim.SetFloat("sides", 0f);
anim.SetFloat("speed", 0f);
agent.avoidancePriority = 30;
agent.enabled = false;
agentObstacle.enabled = true;
}
}
}

bool PathFindingComplete(){
double r = 2;
float distance = Vector3.Distance(agent.destination, agent.transform.position);

if (manager)
{
r += manager.arrivedAtDestination * (manager.arrivedAtDestination) * 30;
}
if (distance <= (agent.stoppingDistance + r))
{
if (!agent.hasPath || agent.velocity.sqrMagnitude <= 0.05f)
{
if (manager)
{
manager.arrivedAtDestination += 1;
if (manager.arrivedAtDestination == manager.selectedCount)
{
manager.arrivedAtDestination = 0;
agent.stoppingDistance = 0.6f;
}
}
return true;
}
}
return false;
}

public void OnSelected(){
gameObject.transform.GetChild(7).gameObject.SetActive(true);
}
public void OnDeselect(){
gameObject.transform.GetChild(7).gameObject.SetActive(false);
}

public void SetAgentDestination(Vector3 targetPosition){
ActivateAgent = true;
newTargetPosition = targetPosition;
agentObstacle.enabled = false;
}

private void OnAnimatorMove()
{
if(!IsJumping && !IsRotating)
transform.position = agent.nextPosition;
}

public void OnSpeedSliderChange(float val)
{
speed = (1 + val * 2) * baseSpeed;
}
}
11 changes: 11 additions & 0 deletions B3 Scripts/AgentController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e735d70

Please sign in to comment.