Added projectile line and goal
This commit is contained in:
@@ -13,10 +13,28 @@ public class FollowCam : MonoBehaviour
|
||||
camZ = this.transform.position.z;
|
||||
}
|
||||
void FixedUpdate() {
|
||||
// if there's only one line following an if, it doesn't need braces
|
||||
if (POI == null) return; // return if there is no poi
|
||||
// Get the position of the poi
|
||||
Vector3 destination = POI.transform.position;
|
||||
//// if there's only one line following an if, it doesn't need braces
|
||||
//if (POI == null) return; // return if there is no poi
|
||||
//// Get the position of the poi
|
||||
//Vector3 destination = POI.transform.position;
|
||||
Vector3 destination;
|
||||
// If there is no poi, return to P:[0,0,0]
|
||||
if (POI == null ) {
|
||||
destination = Vector3.zero;
|
||||
} else {
|
||||
// Get the position of the poi
|
||||
destination = POI. transform.position;
|
||||
// If poi is a Projectile, check to see if it's at rest
|
||||
if (POI.tag == "Projectile") {
|
||||
// If it is sleeping (that is, not moving)
|
||||
if (POI.GetComponent<Rigidbody>().IsSleeping()) {
|
||||
// Return to default view
|
||||
POI = null;
|
||||
// in the next update
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Limit the X & Y to minimum values
|
||||
destination.x = Mathf.Max(minXY.x, destination.x);
|
||||
destination.y = Mathf.Max(minXY.y, destination.y);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Goal : MonoBehaviour
|
||||
{
|
||||
// A static field accessible by code anywhere
|
||||
static public bool goalMet = false;
|
||||
void OnTriggerEnter(Collider other) {
|
||||
// When the trigger is hit by something
|
||||
// Check to see if it's a Projectile
|
||||
if (other.gameObject.tag == "Projectile") {
|
||||
// If so, set goalMet to true
|
||||
Goal.goalMet = true;
|
||||
// Also set the alpha of the color to higher opacity
|
||||
Material mat = GetComponent<Renderer>().material;
|
||||
Color c = mat.color;
|
||||
c.a = 1;
|
||||
mat.color = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c73d4fddf5c603f41996d8355592ac28
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,100 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ProjectileLine : MonoBehaviour
|
||||
{
|
||||
static public ProjectileLine s;
|
||||
[Header("Set in Inspector")]
|
||||
public float minDist = 0.1f;
|
||||
private LineRenderer line;
|
||||
private GameObject _poi;
|
||||
private List<Vector3> points;
|
||||
void Awake() {
|
||||
s = this; // Set the singleton
|
||||
// Get a reference to the LineRenderer
|
||||
line = GetComponent<LineRenderer>();
|
||||
// Disable the LineRenderer until it's needed
|
||||
line.enabled = false;
|
||||
// Initialize the points List
|
||||
points = new List<Vector3>();
|
||||
}
|
||||
// This is a property (that is, amethod masquerading as a field)
|
||||
public GameObject poi {
|
||||
get {
|
||||
return (_poi);
|
||||
}
|
||||
set {
|
||||
_poi = value;
|
||||
if (_poi != null) {
|
||||
// When _poi is set to something new, it resets everything
|
||||
line.enabled = false;
|
||||
points = new List<Vector3>();
|
||||
AddPoint();
|
||||
}
|
||||
}
|
||||
}
|
||||
// This can be used to clear the line directly
|
||||
public void Clear() {
|
||||
_poi = null;
|
||||
line.enabled = false;
|
||||
points = new List<Vector3>();
|
||||
}
|
||||
public void AddPoint() {
|
||||
// This is called to add a point to the line
|
||||
Vector3 pt = _poi.transform.position;
|
||||
if (points.Count > 0 && (pt - lastPoint).magnitude < minDist) {
|
||||
// If the point isn't far enough from the last point, it returns
|
||||
return;
|
||||
}
|
||||
// If this is the launch point...
|
||||
if (points.Count == 0) {
|
||||
Vector3 launchPosDiff = pt - Slingshot.LAUNCH_POS; // To be defined
|
||||
// ...it adds an extra bit of line to aid aiming later
|
||||
points.Add(pt + launchPosDiff);
|
||||
points.Add(pt);
|
||||
line.positionCount = 2;
|
||||
// Sets the first two points
|
||||
line.SetPosition(0, points[0]);
|
||||
line.SetPosition(1, points[1]);
|
||||
// Enables the LineRenderer
|
||||
line.enabled = true;
|
||||
} else {
|
||||
// Normal behavior of adding a point
|
||||
points.Add(pt);
|
||||
line.positionCount = points.Count;
|
||||
line.SetPosition(points.Count-1, lastPoint);
|
||||
line.enabled = true;
|
||||
}
|
||||
}
|
||||
public Vector3 lastPoint {
|
||||
get {
|
||||
if (points == null) {
|
||||
// If there are no points, returns Vector3.zero
|
||||
return (Vector3.zero);
|
||||
}
|
||||
return (points[points.Count-1]);
|
||||
}
|
||||
}
|
||||
void FixedUpdate() {
|
||||
if (poi == null) {
|
||||
// If there is no poi, search for one
|
||||
if (FollowCam.POI != null) {
|
||||
if (FollowCam.POI.tag == "Projectile") {
|
||||
poi = FollowCam.POI;
|
||||
} else {
|
||||
return; // Return if we didn't find a poi
|
||||
}
|
||||
} else {
|
||||
return; // Return if we didn't find a poi
|
||||
}
|
||||
}
|
||||
// If there is a poi, it's loc is added every FixedUpdate
|
||||
AddPoint();
|
||||
if (FollowCam.POI == null) {
|
||||
// Once FollowCam.POI is null, make the local poi null too
|
||||
poi = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a83600b7befe60780b1cb36e69d36e8e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class RigidbodySleep : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
Rigidbody rb = GetComponent<Rigidbody>();
|
||||
if (rb != null) rb.Sleep();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 08200bcd507d3abacadb870d366695e0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -3,6 +3,7 @@ using System.Collections;
|
||||
|
||||
public class Slingshot : MonoBehaviour
|
||||
{
|
||||
static private Slingshot s;
|
||||
// fields set in the Unity Inspector pane
|
||||
[Header("Set in Inspector")]
|
||||
public GameObject prefabProjectile;
|
||||
@@ -14,7 +15,14 @@ public class Slingshot : MonoBehaviour
|
||||
public GameObject projectile;
|
||||
public bool aimingMode;
|
||||
private Rigidbody projectileRigidbody;
|
||||
static public Vector3 LAUNCH_POS {
|
||||
get {
|
||||
if (s == null) return Vector3.zero;
|
||||
return s.launchPos;
|
||||
}
|
||||
}
|
||||
void Awake() {
|
||||
s = this;
|
||||
Transform launchPointTrans = transform.Find("LaunchPoint");
|
||||
launchPoint = launchPointTrans.gameObject;
|
||||
launchPoint.SetActive(false);
|
||||
|
||||
Reference in New Issue
Block a user