2026-05-21 스킬예측(진행중)

This commit is contained in:
2026-05-21 18:14:04 +09:00
parent 04d4939e88
commit b9338b04f1
2479 changed files with 259780 additions and 20 deletions

View File

@@ -0,0 +1,103 @@
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System;
using UnityEngine;
public class HS_CameraController : MonoBehaviour
{
//camera holder
public Transform Holder;
public Vector3 cameraPos = new Vector3(0, 0, 0);
public float currDistance = 5.0f;
public float xRotate = 250.0f;
public float yRotate = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float prevDistance;
private float x = 0.0f;
private float y = 0.0f;
//For camera colliding
RaycastHit hit;
public LayerMask collidingLayers = ~0; //Target marker can only collide with scene layer
private float distanceHit;
void Start()
{
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
void LateUpdate()
{
if (currDistance < 2)
{
currDistance = 2;
}
// (currDistance - 2) / 3.5f - constant for far camera position
var targetPos = Holder.position + new Vector3(0, (distanceHit - 2) / 3f + cameraPos[1], 0);
currDistance -= Input.GetAxis("Mouse ScrollWheel") * 2;
if (Holder)
{
var pos = Input.mousePosition;
float dpiScale = 1;
if (Screen.dpi < 1) dpiScale = 1;
if (Screen.dpi < 200) dpiScale = 1;
else dpiScale = Screen.dpi / 200f;
if (pos.x < 380 * dpiScale && Screen.height - pos.y < 250 * dpiScale) return;
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
x += (float)(Input.GetAxis("Mouse X") * xRotate * 0.02);
y -= (float)(Input.GetAxis("Mouse Y") * yRotate * 0.02);
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * new Vector3(cameraPos[2], 0, -currDistance) + targetPos;
//If camera collide with collidingLayers move it to this point.
if (Physics.Raycast(targetPos, position - targetPos, out hit, (position - targetPos).magnitude, collidingLayers))
{
transform.position = hit.point;
//Min(4) distance from ground for camera target point
distanceHit = Mathf.Clamp(Vector3.Distance(targetPos, hit.point), 4, 600);
}
else
{
transform.position = position;
distanceHit = currDistance;
}
transform.rotation = rotation;
}
else
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
if (prevDistance != currDistance)
{
prevDistance = currDistance;
var rot = Quaternion.Euler(y, x, 0);
// (currDistance - 2) / 3.5f - constant for far camera position
var po = rot * new Vector3(cameraPos[2], 0, -currDistance) + targetPos;
transform.rotation = rot;
transform.position = po;
}
}
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
{
angle += 360;
}
if (angle > 360)
{
angle -= 360;
}
return Mathf.Clamp(angle, min, max);
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: a4ca5efb641d8634a9ec2fdc65f1f7d2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 315729
packageName: BIG Projectiles bundle
packageVersion: 3.1
assetPath: Assets/Hovl Studio/HSFiles/Scripts/For demo scenes/HS_CameraController.cs
uploadId: 883376

View File

@@ -0,0 +1,76 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HS_CameraShaker : MonoBehaviour
{
public Transform cameraObject;
public float amplitude;
public float frequency;
public float duration;
public float timeRemaining;
private Vector3 noiseOffset;
private Vector3 noise;
private AnimationCurve smoothCurve = new AnimationCurve(new Keyframe(0.0f, 0.0f, Mathf.Deg2Rad * 0.0f, Mathf.Deg2Rad * 720.0f), new Keyframe(0.2f, 1.0f), new Keyframe(1.0f, 0.0f));
void Start()
{
float rand = 32.0f;
noiseOffset.x = Random.Range(0.0f, rand);
noiseOffset.y = Random.Range(0.0f, rand);
noiseOffset.z = Random.Range(0.0f, rand);
}
public IEnumerator Shake(float amp, float freq, float dur, float wait)
{
yield return new WaitForSeconds(wait);
float rand = 32.0f;
noiseOffset.x = Random.Range(0.0f, rand);
noiseOffset.y = Random.Range(0.0f, rand);
noiseOffset.z = Random.Range(0.0f, rand);
amplitude = amp;
frequency = freq;
duration = dur;
timeRemaining += dur;
if (timeRemaining > dur)
{
timeRemaining = dur;
}
}
void Update()
{
if (timeRemaining <= 0)
return;
float deltaTime = Time.deltaTime;
timeRemaining -= deltaTime;
float noiseOffsetDelta = deltaTime * frequency;
noiseOffset.x += noiseOffsetDelta;
noiseOffset.y += noiseOffsetDelta;
noiseOffset.z += noiseOffsetDelta;
noise.x = Mathf.PerlinNoise(noiseOffset.x, 0.0f);
noise.y = Mathf.PerlinNoise(noiseOffset.y, 1.0f);
noise.z = Mathf.PerlinNoise(noiseOffset.z, 2.0f);
noise -= Vector3.one * 0.5f;
noise *= amplitude;
float agePercent = 1.0f - (timeRemaining / duration);
noise *= smoothCurve.Evaluate(agePercent);
}
void LateUpdate()
{
if (timeRemaining <= 0)
return;
Vector3 positionOffset = Vector3.zero;
Vector3 rotationOffset = Vector3.zero;
positionOffset += noise;
rotationOffset += noise;
cameraObject.transform.localPosition = positionOffset;
cameraObject.transform.localEulerAngles = rotationOffset;
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: c9da73e1bfedb57418eb344344877d2f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 315729
packageName: BIG Projectiles bundle
packageVersion: 3.1
assetPath: Assets/Hovl Studio/HSFiles/Scripts/For demo scenes/HS_CameraShaker.cs
uploadId: 883376

View File

@@ -0,0 +1,355 @@
using System.Collections.Generic;
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
namespace Hovl
{
public class HS_DemoShooting : MonoBehaviour
{
[Header("Fire rate")]
[Range(0.01f, 1f)]
public float fireRate = 0.1f;
float fireCountdown;
[Header("References")]
[SerializeField] Transform firePoint;
[SerializeField] Camera cam;
[SerializeField] Animation camAnim;
[Header("Projectile settings")]
[SerializeField] float maxLength = 100f;
[SerializeField] GameObject[] prefabs;
[Header("Pooling")]
[SerializeField] int maxPoolSizePerPrefab = 40;
[Header("Projectile switching")]
[SerializeField] float switchDelay = 0.4f;
int currentPrefabIndex;
float buttonSaver;
static Transform globalPoolRoot;
// key = prefab instance id, value = pool list
static readonly Dictionary<int, List<GameObject>> pools = new Dictionary<int, List<GameObject>>();
static readonly Dictionary<int, Transform> poolParents = new Dictionary<int, Transform>();
void Awake()
{
EnsureGlobalPool();
if (cam == null)
cam = Camera.main;
}
void Start()
{
Counter(0);
}
void Update()
{
HandleShooting();
HandleProjectileSwitch();
HandleRotation();
if (fireCountdown > 0f)
fireCountdown -= Time.deltaTime;
buttonSaver += Time.deltaTime;
}
void HandleShooting()
{
if (IsFirePressedThisFrame())
{
Shoot();
}
if (IsFastFireHeld() && fireCountdown <= 0f)
{
Shoot();
fireCountdown = fireRate;
}
}
void HandleProjectileSwitch()
{
float horizontal = GetHorizontalInput();
if (horizontal < 0f && buttonSaver >= switchDelay)
{
buttonSaver = 0f;
Counter(-1);
}
else if (horizontal > 0f && buttonSaver >= switchDelay)
{
buttonSaver = 0f;
Counter(1);
}
}
void HandleRotation()
{
if (cam == null)
return;
Vector2 pointerPosition = GetPointerScreenPosition();
Ray ray = cam.ScreenPointToRay(pointerPosition);
if (Physics.Raycast(ray, out RaycastHit hit, maxLength))
{
RotateToMouseDirection(hit.point);
}
}
void Shoot()
{
if (prefabs == null || prefabs.Length == 0)
return;
if (firePoint == null)
{
Debug.LogWarning("HS_DemoShooting: FirePoint is not assigned.");
return;
}
GameObject prefab = prefabs[currentPrefabIndex];
if (prefab == null)
return;
GameObject projectile = GetProjectile(prefab);
if (projectile == null)
return;
if (camAnim != null && camAnim.clip != null)
camAnim.Play(camAnim.clip.name);
Transform projectileTransform = projectile.transform;
projectileTransform.SetParent(null, false);
projectileTransform.SetPositionAndRotation(firePoint.position, firePoint.rotation);
Rigidbody rb = projectile.GetComponent<Rigidbody>();
if (rb != null)
{
#if UNITY_6000_0_OR_NEWER
rb.linearVelocity = Vector3.zero;
#else
rb.velocity = Vector3.zero;
#endif
rb.angularVelocity = Vector3.zero;
}
projectile.SetActive(true);
IPooledProjectile pooledProjectile = projectile.GetComponent<IPooledProjectile>();
if (pooledProjectile != null)
pooledProjectile.OnSpawnedFromPool();
}
GameObject GetProjectile(GameObject prefab)
{
if (prefab == null)
return null;
int prefabId = prefab.GetInstanceID();
if (!pools.TryGetValue(prefabId, out List<GameObject> pool))
{
pool = new List<GameObject>();
pools[prefabId] = pool;
}
CleanupDestroyedObjects(pool);
for (int i = 0; i < pool.Count; i++)
{
GameObject pooledObject = pool[i];
if (pooledObject == null)
continue;
if (!pooledObject.activeInHierarchy)
return pooledObject;
}
if (GetValidObjectCount(pool) >= maxPoolSizePerPrefab)
return null;
GameObject newProjectile = CreateProjectile(prefab, prefabId);
if (newProjectile != null)
pool.Add(newProjectile);
return newProjectile;
}
void CleanupDestroyedObjects(List<GameObject> pool)
{
for (int i = pool.Count - 1; i >= 0; i--)
{
if (pool[i] == null)
pool.RemoveAt(i);
}
}
int GetValidObjectCount(List<GameObject> pool)
{
int count = 0;
for (int i = 0; i < pool.Count; i++)
{
if (pool[i] != null)
count++;
}
return count;
}
GameObject CreateProjectile(GameObject prefab, int prefabId)
{
Transform parent = GetOrCreatePoolParent(prefab, prefabId);
GameObject newProjectile = Instantiate(prefab, parent);
newProjectile.SetActive(false);
return newProjectile;
}
Transform GetOrCreatePoolParent(GameObject prefab, int prefabId)
{
if (poolParents.TryGetValue(prefabId, out Transform existingParent) && existingParent != null)
return existingParent;
GameObject parentObject = new GameObject(prefab.name + "_Pool");
parentObject.transform.SetParent(globalPoolRoot);
poolParents[prefabId] = parentObject.transform;
return parentObject.transform;
}
static void EnsureGlobalPool()
{
if (globalPoolRoot != null)
return;
GameObject existing = GameObject.Find("Hovl_GlobalProjectilePool");
if (existing != null)
{
globalPoolRoot = existing.transform;
DontDestroyOnLoad(existing);
return;
}
GameObject poolObject = new GameObject("Hovl_GlobalProjectilePool");
DontDestroyOnLoad(poolObject);
globalPoolRoot = poolObject.transform;
}
void Counter(int count)
{
if (prefabs == null || prefabs.Length == 0)
return;
currentPrefabIndex += count;
if (currentPrefabIndex >= prefabs.Length)
currentPrefabIndex = 0;
else if (currentPrefabIndex < 0)
currentPrefabIndex = prefabs.Length - 1;
}
void RotateToMouseDirection(Vector3 destination)
{
Vector3 direction = destination - transform.position;
if (direction.sqrMagnitude <= 0.0001f)
return;
transform.rotation = Quaternion.LookRotation(direction);
}
bool IsFirePressedThisFrame()
{
#if ENABLE_INPUT_SYSTEM
if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame)
return true;
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
if (Input.GetButtonDown("Fire1"))
return true;
#endif
return false;
}
bool IsFastFireHeld()
{
#if ENABLE_INPUT_SYSTEM
if (Mouse.current != null && Mouse.current.rightButton.isPressed)
return true;
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
if (Input.GetMouseButton(1))
return true;
#endif
return false;
}
float GetHorizontalInput()
{
float horizontal = 0f;
#if ENABLE_INPUT_SYSTEM
if (Keyboard.current != null)
{
if (Keyboard.current.aKey.isPressed)
horizontal -= 1f;
if (Keyboard.current.dKey.isPressed)
horizontal += 1f;
}
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
if (Mathf.Approximately(horizontal, 0f))
{
if (Input.GetKey(KeyCode.A))
horizontal -= 1f;
if (Input.GetKey(KeyCode.D))
horizontal += 1f;
if (Mathf.Approximately(horizontal, 0f))
horizontal = Input.GetAxisRaw("Horizontal");
}
#endif
return Mathf.Clamp(horizontal, -1f, 1f);
}
Vector2 GetPointerScreenPosition()
{
#if ENABLE_INPUT_SYSTEM
if (Mouse.current != null)
return Mouse.current.position.ReadValue();
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.mousePosition;
#else
return Vector2.zero;
#endif
}
}
public interface IPooledProjectile
{
void OnSpawnedFromPool();
}
}

View File

@@ -0,0 +1,19 @@
fileFormatVersion: 2
guid: 0015164b45b01e24499396ed322983f5
timeCreated: 1536001444
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 315729
packageName: BIG Projectiles bundle
packageVersion: 3.1
assetPath: Assets/Hovl Studio/HSFiles/Scripts/For demo scenes/HS_DemoShooting.cs
uploadId: 883376

View File

@@ -0,0 +1,101 @@
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System;
using UnityEngine;
public class HS_DemoShooting2D : MonoBehaviour
{
public GameObject FirePoint;
public Camera Cam;
public float MaxLength;
public GameObject[] Prefabs;
private Ray RayMouse;
private Vector3 direction;
private Quaternion rotation;
[Header("GUI")]
private float windowDpi;
private int Prefab;
private GameObject Instance;
private float hSliderValue = 0.1f;
private float fireCountdown = 0f;
//Double-click protection
private float buttonSaver = 0f;
void Start()
{
if (Screen.dpi < 1) windowDpi = 1;
if (Screen.dpi < 200) windowDpi = 1;
else windowDpi = Screen.dpi / 200f;
Counter(0);
}
void Update()
{
//Single shoot
if (Input.GetButtonDown("Fire1"))
{
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
}
//Fast shooting
if (Input.GetMouseButton(1) && fireCountdown <= 0f)
{
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
fireCountdown = 0;
fireCountdown += hSliderValue;
}
fireCountdown -= Time.deltaTime;
//To change projectiles
if ((Input.GetKey(KeyCode.A) || Input.GetAxis("Horizontal") < 0) && buttonSaver >= 0.4f)// left button
{
buttonSaver = 0f;
Counter(-1);
}
if ((Input.GetKey(KeyCode.D) || Input.GetAxis("Horizontal") > 0) && buttonSaver >= 0.4f)// right button
{
buttonSaver = 0f;
Counter(+1);
}
buttonSaver += Time.deltaTime;
//To rotate fire point
if (Cam != null)
{
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
}
else
{
Debug.Log("No camera");
}
}
//GUI Text
void OnGUI()
{
GUI.Label(new Rect(10 * windowDpi, 5 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use left mouse button to single shoot!");
GUI.Label(new Rect(10 * windowDpi, 25 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use and hold the right mouse button for quick shooting!");
GUI.Label(new Rect(10 * windowDpi, 45 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Fire rate:");
hSliderValue = GUI.HorizontalSlider(new Rect(70 * windowDpi, 50 * windowDpi, 100 * windowDpi, 20 * windowDpi), hSliderValue, 0.0f, 1.0f);
GUI.Label(new Rect(10 * windowDpi, 65 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use the keyboard buttons A/<- and D/-> to change projectiles!");
}
// To change prefabs (count - prefab number)
void Counter(int count)
{
Prefab += count;
if (Prefab > Prefabs.Length - 1)
{
Prefab = 0;
}
else if (Prefab < 0)
{
Prefab = Prefabs.Length - 1;
}
}
}

View File

@@ -0,0 +1,19 @@
fileFormatVersion: 2
guid: 6cedae4990f69554199024f64462bd8a
timeCreated: 1536001444
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 315729
packageName: BIG Projectiles bundle
packageVersion: 3.1
assetPath: Assets/Hovl Studio/HSFiles/Scripts/For demo scenes/HS_DemoShooting2D.cs
uploadId: 883376

View File

@@ -0,0 +1,292 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//This script requires you to have setup your animator with 3 parameters, "InputMagnitude", "InputX", "InputZ"
//With a blend tree to control the inputmagnitude and allow blending between animations.
//Also you need to shoose Firepoint, targets > 1, Aim image from canvas and 2 target markers and camera.
[RequireComponent(typeof(CharacterController))]
public class HS_TargetProjectiles : MonoBehaviour
{
public float velocity = 9;
[Space]
public float InputX;
public float InputZ;
public Vector3 desiredMoveDirection;
public bool blockRotationPlayer;
public float desiredRotationSpeed = 0.1f;
public Animator anim;
public float Speed;
public float allowPlayerRotation = 0.1f;
public Camera cam;
public CharacterController controller;
public bool isGrounded;
private float secondLayerWeight = 0;
[Space]
[Header("Animation Smoothing")]
[Range(0, 1f)]
public float HorizontalAnimSmoothTime = 0.2f;
[Range(0, 1f)]
public float VerticalAnimTime = 0.2f;
[Range(0, 1f)]
public float StartAnimTime = 0.3f;
[Range(0, 1f)]
public float StopAnimTime = 0.15f;
private float verticalVel;
private Vector3 moveVector;
[Space]
[Header("Effects")]
public GameObject[] Prefabs;
public GameObject[] PrefabsCast;
private ParticleSystem Effect;
private int prefabNumber;
private Transform parentObject;
public LayerMask collidingLayer = ~0; //Target marker can only collide with scene layer
[Space]
[Header("Canvas")]
public Image aim;
public Vector2 uiOffset;
public List<Transform> screenTargets = new List<Transform>();
private Transform target;
private bool activeTarger = false;
public Transform FirePoint;
public float fireRate = 0.1f;
private float fireCountdown = 0f;
private bool rotateState = false;
[Space]
[Header("Sound effects")]
private AudioSource soundComponent; //Play audio from Prefabs
private AudioClip clip;
[Space]
[Header("Camera Shaker script")]
public HS_CameraShaker cameraShaker;
void Start()
{
anim = this.GetComponent<Animator>();
cam = Camera.main;
controller = this.GetComponent<CharacterController>();
//Get clip from Audiosource from projectile if exist for playing when shooting
if (PrefabsCast[0].GetComponent<AudioSource>())
{
soundComponent = PrefabsCast[0].GetComponent<AudioSource>();
}
}
void Update()
{
target = screenTargets[TargetIndex()];
if (Input.GetMouseButtonDown(2))
{
Counter(-1);
}
if (Input.GetMouseButtonDown(1))
{
Counter(+1);
}
UserInterface();
if (Input.GetMouseButton(0) && activeTarger)
{
if (rotateState == false)
{
StartCoroutine(RotateToTarget(fireRate, target.position));
}
secondLayerWeight = Mathf.Lerp(secondLayerWeight, 1, Time.deltaTime * 10);
if (fireCountdown <= 0f)
{
GameObject projectile = Instantiate(Prefabs[prefabNumber], FirePoint.position, FirePoint.rotation);
projectile.GetComponent<HS_TargetProjectile>().UpdateTarget(target, (Vector3)uiOffset);
Effect = PrefabsCast[prefabNumber].GetComponent<ParticleSystem>();
Effect.Play();
//Get Audiosource from Prefabs if exist
if (PrefabsCast[prefabNumber].GetComponent<AudioSource>())
{
soundComponent = PrefabsCast[prefabNumber].GetComponent<AudioSource>();
clip = soundComponent.clip;
soundComponent.PlayOneShot(clip);
}
StartCoroutine(cameraShaker.Shake(0.1f, 2, 0.2f, 0));
fireCountdown = 0;
fireCountdown += fireRate;
}
}
else
{
secondLayerWeight = Mathf.Lerp(secondLayerWeight, 0, Time.deltaTime * 10);
}
fireCountdown -= Time.deltaTime;
//Need second layer in the Animator
if (anim.layerCount > 1) { anim.SetLayerWeight(1, secondLayerWeight); }
InputMagnitude();
//If you don't need the character grounded then get rid of this part.
isGrounded = controller.isGrounded;
if (isGrounded)
{
verticalVel = 0;
}
else
{
verticalVel -= 1f * Time.deltaTime;
}
moveVector = new Vector3(0, verticalVel, 0);
controller.Move(moveVector);
}
void Counter(int count)
{
prefabNumber += count;
if (prefabNumber > Prefabs.Length - 1)
{
prefabNumber = 0;
}
else if (prefabNumber < 0)
{
prefabNumber = Prefabs.Length - 1;
}
}
private void UserInterface()
{
Vector3 screenCenter = new Vector3(Screen.width / 1.4f, Screen.height / 2, 0);
Vector3 screenPos = Camera.main.WorldToScreenPoint(target.position + (Vector3)uiOffset);
Vector3 CornerDistance = screenPos - screenCenter;
Vector3 absCornerDistance = new Vector3(Mathf.Abs(CornerDistance.x), Mathf.Abs(CornerDistance.y), Mathf.Abs(CornerDistance.z));
//This way you can find target on the full screen
//if (screenPos.z > 0 && screenPos.x > 0 && screenPos.x < Screen.width && screenPos.y > 0 && screenPos.y < Screen.height)
// {screenPos.x > 0 && screenPos.y > 0 && screenPos.z > 0} - disable target if enemy backside
//Find target near center of the screen
if (absCornerDistance.x < screenCenter.x / 3 && absCornerDistance.y < screenCenter.y / 3 && screenPos.x > 0 && screenPos.y > 0 && screenPos.z > 0 //If target is in the middle-right of the screen
&& !Physics.Linecast(transform.position + (Vector3)uiOffset, target.position + (Vector3)uiOffset * 2, collidingLayer)) //If player can see the target
{
aim.transform.position = Vector3.MoveTowards(aim.transform.position, screenPos, Time.deltaTime * 3000);
if (!activeTarger)
activeTarger = true;
}
else
{
//Another way
//aim.GetComponent<RectTransform>().localPosition = new Vector3(0, 0, 0);
aim.transform.position = Vector3.MoveTowards(aim.transform.position, screenCenter, Time.deltaTime * 3000);
if (activeTarger)
activeTarger = false;
}
}
//Rotate player to target when attack
public IEnumerator RotateToTarget(float rotatingTime, Vector3 targetPoint)
{
rotateState = true;
float delay = rotatingTime;
var lookPos = targetPoint - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
while (true)
{
if (Speed == 0) { transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * 20); }
delay -= Time.deltaTime;
if (delay <= 0 || transform.rotation == rotation)
{
rotateState = false;
yield break;
}
yield return null;
}
}
void PlayerMoveAndRotation()
{
InputX = Input.GetAxis("Horizontal");
InputZ = Input.GetAxis("Vertical");
var camera = Camera.main;
var forward = cam.transform.forward;
var right = cam.transform.right;
forward.y = 0f;
right.y = 0f;
forward.Normalize();
right.Normalize();
//Movement vector
desiredMoveDirection = forward * InputZ + right * InputX;
//Character diagonal movement faster fix
desiredMoveDirection.Normalize();
if (blockRotationPlayer == false)
{
//You can use desiredMoveDirection if using InputMagnitude instead of Horizontal&Vertical axis
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(forward), desiredRotationSpeed);
//Limit back speed
if (InputZ < -0.5)
controller.Move(desiredMoveDirection * Time.deltaTime * (velocity / 1.5f));
//else if (InputX < -0.1 || InputX > 0.1)
// controller.Move(desiredMoveDirection * Time.deltaTime * (velocity / 1.2f));
else
controller.Move(desiredMoveDirection * Time.deltaTime * velocity);
}
}
void InputMagnitude()
{
//Calculate Input Vectors
InputX = Input.GetAxis("Horizontal");
InputZ = Input.GetAxis("Vertical");
anim.SetFloat("InputZ", InputZ, VerticalAnimTime, Time.deltaTime * 2f);
anim.SetFloat("InputX", InputX, HorizontalAnimSmoothTime, Time.deltaTime * 2f);
//Calculate the Input Magnitude
Speed = new Vector2(InputX, InputZ).sqrMagnitude;
//Physically move player
if (Speed > allowPlayerRotation)
{
anim.SetFloat("InputMagnitude", Speed, StartAnimTime, Time.deltaTime);
PlayerMoveAndRotation();
}
else if (Speed < allowPlayerRotation)
{
anim.SetFloat("InputMagnitude", Speed, StopAnimTime, Time.deltaTime);
}
}
public int TargetIndex()
{
float[] distances = new float[screenTargets.Count];
for (int i = 0; i < screenTargets.Count; i++)
{
distances[i] = Vector2.Distance(Camera.main.WorldToScreenPoint(screenTargets[i].position), new Vector2(Screen.width / 1.4f, Screen.height / 2));
}
float minDistance = Mathf.Min(distances);
int index = 0;
for (int i = 0; i < distances.Length; i++)
{
if (minDistance == distances[i])
index = i;
}
return index;
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: b829ec532fb45434db685458e161d024
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 315729
packageName: BIG Projectiles bundle
packageVersion: 3.1
assetPath: Assets/Hovl Studio/HSFiles/Scripts/For demo scenes/HS_TargetProjectiles.cs
uploadId: 883376