Added clouds and followcam
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Cloud : MonoBehaviour
|
||||
{
|
||||
[Header("Set in Inspector")]
|
||||
public GameObject cloudSphere;
|
||||
public int numSpheresMin = 6;
|
||||
public int numSpheresMax = 10;
|
||||
public Vector3 sphereOffsetScale = new Vector3(5,2,1);
|
||||
public Vector2 sphereScaleRangeX = new Vector2(4,8);
|
||||
public Vector2 sphereScaleRangeY = new Vector2(3,4);
|
||||
public Vector2 sphereScaleRangeZ = new Vector2(2,4);
|
||||
public float scaleYMin = 2f;
|
||||
private List<GameObject> spheres;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
spheres = new List<GameObject>();
|
||||
int num = Random.Range(numSpheresMin, numSpheresMax);
|
||||
for (int i = 0; i < num; i++) {
|
||||
GameObject sp = Instantiate<GameObject>(cloudSphere);
|
||||
spheres.Add(sp);
|
||||
Transform spTrans = sp.transform;
|
||||
spTrans.SetParent(this.transform);
|
||||
// Randomly assign a position
|
||||
Vector3 offset = Random.insideUnitSphere;
|
||||
offset.x *= sphereOffsetScale.x;
|
||||
offset.y *= sphereOffsetScale.y;
|
||||
offset.z *= sphereOffsetScale.z;
|
||||
spTrans.localPosition = offset;
|
||||
// Randomly assign scale
|
||||
Vector3 scale = Vector3.one;
|
||||
scale.x = Random.Range(sphereScaleRangeX.x, sphereScaleRangeX.y);
|
||||
scale.y = Random.Range(sphereScaleRangeY.x, sphereScaleRangeY.y);
|
||||
scale.z = Random.Range(sphereScaleRangeZ.x, sphereScaleRangeZ.y);
|
||||
// Adjust y scale by x distance from core
|
||||
scale.y *= 1 - (Mathf.Abs(offset.x) / sphereOffsetScale.x);
|
||||
scale.y = Mathf.Max(scale.y, scaleYMin);
|
||||
spTrans.localScale = scale;
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
//if (Input.GetKeyDown(KeyCode.Space)) {
|
||||
// Restart();
|
||||
//}
|
||||
}
|
||||
void Restart() {
|
||||
// Clear out old spheres
|
||||
foreach (GameObject sp in spheres) {
|
||||
Destroy(sp);
|
||||
}
|
||||
Start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f76c184ad3c2bda52b334b0ca1447d5d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,65 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class CloudCrafter : MonoBehaviour
|
||||
{
|
||||
[Header("Set in Inspector")]
|
||||
public int numClouds = 40; // The # of clouds to make
|
||||
public GameObject cloudPrefab ; // The prefab for the clouds
|
||||
public Vector3 cloudPosMin = new Vector3(-50,-5,10);
|
||||
public Vector3 cloudPosMax = new Vector3(150,100,10);
|
||||
public float cloudScaleMin = 1; // Max scale of each cloud
|
||||
public float cloudScaleMax = 3; // Max scale of each cloud
|
||||
public float cloudSpeedMult = 0.5f;
|
||||
private GameObject[] cloudInstances;
|
||||
void Awake() {
|
||||
// Make an array large enough to hold all the Cloud instances
|
||||
cloudInstances = new GameObject[numClouds];
|
||||
// Find the CloudAnchor parent GameObject
|
||||
GameObject anchor = GameObject.Find("CloudAnchor");
|
||||
// Iterate through and make Clouds
|
||||
GameObject cloud;
|
||||
for (int i = 0; i < numClouds; i++) {
|
||||
// Make an instance of cloudPrefab
|
||||
cloud = Instantiate<GameObject>(cloudPrefab);
|
||||
// Position cloud
|
||||
Vector3 cPos = Vector3.zero;
|
||||
cPos.x = Random.Range(cloudPosMin.x, cloudPosMax.x);
|
||||
cPos.y = Random.Range(cloudPosMin.y, cloudPosMax.y);
|
||||
// Scale cloud
|
||||
float scaleU = Random.value;
|
||||
float scaleVal = Mathf.Lerp(cloudScaleMin, cloudScaleMax, scaleU);
|
||||
// Smaller clouds (with smaller scaleU) should be nearer the ground
|
||||
cPos.y = Mathf.Lerp(cloudPosMin.y, cPos.y, scaleU);
|
||||
// Smaller cloud should be further away
|
||||
cPos.z = 100 - 90 * scaleU;
|
||||
// Apply these transforms to the cloud
|
||||
cloud.transform.position = cPos;
|
||||
cloud.transform.localScale = Vector3.one * scaleVal;
|
||||
// Make cloud a child of the anchor
|
||||
cloud.transform.SetParent(anchor.transform);
|
||||
// Add the cloud to cloudInstances
|
||||
cloudInstances[i] = cloud;
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
// Iterate over each cloud that was create
|
||||
foreach (GameObject cloud in cloudInstances) {
|
||||
// Get the cloud scale and position
|
||||
float scaleVal = cloud.transform.localScale.x;
|
||||
Vector3 cPos = cloud.transform.position;
|
||||
// Move larger clouds faster
|
||||
cPos.x -= scaleVal * Time.deltaTime * cloudSpeedMult;
|
||||
// If a cloud has moved too far to the left...
|
||||
if (cPos.x <= cloudPosMin.x) {
|
||||
// Move it to the far right
|
||||
cPos.x = cloudPosMax.x;
|
||||
}
|
||||
// Apply the new position to cloud
|
||||
cloud.transform.position = cPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ce3134b55d3319a479f8fc97faf1fd0a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class FollowCam : MonoBehaviour
|
||||
{
|
||||
static public GameObject POI; // The static point of interest
|
||||
[Header("Set in Inspector")]
|
||||
public float easing = 0.05f;
|
||||
public Vector2 minXY = Vector2.zero;
|
||||
[Header("Set Dynamically")]
|
||||
public float camZ; // The desired Z pos of the camera
|
||||
void Awake() {
|
||||
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;
|
||||
// Limit the X & Y to minimum values
|
||||
destination.x = Mathf.Max(minXY.x, destination.x);
|
||||
destination.y = Mathf.Max(minXY.y, destination.y);
|
||||
// Interpolate from the current Camera position toward destination
|
||||
destination = Vector3.Lerp(transform.position, destination, easing);
|
||||
// Force destination.z to be camZ to keep the camera far enough away
|
||||
destination.z = camZ;
|
||||
// Set the camera to the destination
|
||||
transform.position = destination;
|
||||
// Set the orthographicSize of the Camera to keep Ground in view
|
||||
Camera.main.orthographicSize = destination.y + 10;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 351e76b062f02288684a52e9520a2f4f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -3,10 +3,69 @@ using System.Collections;
|
||||
|
||||
public class Slingshot : MonoBehaviour
|
||||
{
|
||||
// fields set in the Unity Inspector pane
|
||||
[Header("Set in Inspector")]
|
||||
public GameObject prefabProjectile;
|
||||
public float velocityMult = 8f;
|
||||
// fields set dynamically
|
||||
[Header("Set Dynamically")]
|
||||
public GameObject launchPoint;
|
||||
public Vector3 launchPos;
|
||||
public GameObject projectile;
|
||||
public bool aimingMode;
|
||||
private Rigidbody projectileRigidbody;
|
||||
void Awake() {
|
||||
Transform launchPointTrans = transform.Find("LaunchPoint");
|
||||
launchPoint = launchPointTrans.gameObject;
|
||||
launchPoint.SetActive(false);
|
||||
launchPos = launchPointTrans.position;
|
||||
}
|
||||
void OnMouseEnter() {
|
||||
print("Slingshot:OnMouseEnter()");
|
||||
//print("Slingshot:OnMouseEnter()");
|
||||
launchPoint.SetActive(true);
|
||||
}
|
||||
void OnMouseExit() {
|
||||
print("Slingshot:OnMouseExit()");
|
||||
//print("Slingshot:OnMouseExit()");
|
||||
launchPoint.SetActive(false);
|
||||
}
|
||||
void OnMouseDown() {
|
||||
// The player has pressed the mouse button while over Slingshot
|
||||
aimingMode = true;
|
||||
// Instantiate a projectile
|
||||
projectile = Instantiate(prefabProjectile) as GameObject;
|
||||
// Start it at the launchPoint
|
||||
projectile.transform.position = launchPos;
|
||||
// Set it to isKinematic for now
|
||||
projectile.GetComponent<Rigidbody>().isKinematic = true;
|
||||
// Set it to isKinematic for now
|
||||
projectileRigidbody = projectile.GetComponent<Rigidbody>();
|
||||
projectileRigidbody.isKinematic = true;
|
||||
}
|
||||
void Update() {
|
||||
// If Slingshot is not in aimingMode, don't run this code
|
||||
if (!aimingMode) return;
|
||||
// Get the current mouse position in 2D screen coordinates
|
||||
Vector3 mousePos2D = Input.mousePosition;
|
||||
mousePos2D.z = -Camera.main.transform.position.z;
|
||||
Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D);
|
||||
// Find the delta from the launchPos to the mousePos3D
|
||||
Vector3 mouseDelta = mousePos3D - launchPos;
|
||||
// Limit mouseDelta to the radius of the Slingshot SphereCollider
|
||||
float maxMagnitude = this.GetComponent<SphereCollider>().radius;
|
||||
if (mouseDelta.magnitude > maxMagnitude) {
|
||||
mouseDelta.Normalize();
|
||||
mouseDelta *= maxMagnitude;
|
||||
}
|
||||
// Move the projectile to this new position
|
||||
Vector3 projPos = launchPos + mouseDelta;
|
||||
projectile.transform.position = projPos;
|
||||
if (Input.GetMouseButtonUp(0)) {
|
||||
// The mouse has been released
|
||||
aimingMode = false;
|
||||
projectileRigidbody.isKinematic = false;
|
||||
projectileRigidbody.velocity = -mouseDelta * velocityMult;
|
||||
FollowCam.POI = projectile;
|
||||
projectile = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user