mirror of
https://github.com/ConjureETS/PillowFight.git
synced 2026-03-24 09:00:58 +00:00
add auto target and locking with a player
This commit is contained in:
parent
c484137289
commit
ef8624740c
Binary file not shown.
52
Assets/Scripts/AutoTarget.cs
Normal file
52
Assets/Scripts/AutoTarget.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class AutoTarget : MonoBehaviour {
|
||||
|
||||
private List<Transform> targets;
|
||||
public float minAngleRange = 60f;
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
targets = new List<Transform>();
|
||||
GameObject[] gos = GameObject.FindGameObjectsWithTag("Player");
|
||||
foreach (GameObject go in gos) {
|
||||
if( !go.Equals(gameObject) ){
|
||||
targets.Add(go.transform);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
|
||||
public Transform GetTarget(Vector3 lookingAngle) {
|
||||
|
||||
Transform closest = null;
|
||||
float minAngle = minAngleRange;
|
||||
|
||||
foreach (Transform t in targets) {
|
||||
Vector3 targetDirection = t.transform.position - transform.position;
|
||||
|
||||
float dot = Vector3.Dot(targetDirection, lookingAngle);
|
||||
float angle = Mathf.Acos(dot) * Mathf.Rad2Deg;
|
||||
|
||||
if (angle < minAngle) {
|
||||
minAngle = angle;
|
||||
closest = t;
|
||||
}
|
||||
Debug.Log("angle: " + angle);
|
||||
|
||||
|
||||
}
|
||||
|
||||
Debug.Log("min angle:" + minAngle);
|
||||
|
||||
return closest;
|
||||
}
|
||||
|
||||
}
|
||||
12
Assets/Scripts/AutoTarget.cs.meta
Normal file
12
Assets/Scripts/AutoTarget.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 567fe96dcacb08f4ab716a98ce99fc88
|
||||
timeCreated: 1440221488
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -13,6 +13,7 @@ public class Child : MonoBehaviour
|
||||
private float _xValue;
|
||||
private float _zValue;
|
||||
public Pillow pillow;
|
||||
public Transform target;
|
||||
|
||||
|
||||
void Awake()
|
||||
@ -23,8 +24,13 @@ public class Child : MonoBehaviour
|
||||
void Update()
|
||||
{
|
||||
_isGrounded = IsGrounded();
|
||||
|
||||
// look at the target
|
||||
if (target != null) {
|
||||
transform.LookAt(target);
|
||||
}
|
||||
|
||||
Debug.Log(_isGrounded);
|
||||
//Debug.Log(_isGrounded);
|
||||
}
|
||||
|
||||
|
||||
@ -36,7 +42,7 @@ public class Child : MonoBehaviour
|
||||
|
||||
// TODO: place the pillow correctly or animate or something...
|
||||
|
||||
Debug.Log(_isGrounded);
|
||||
//Debug.Log(_isGrounded);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@ using System.Collections;
|
||||
using InputHandler;
|
||||
|
||||
[RequireComponent(typeof(Child))]
|
||||
[RequireComponent(typeof(AutoTarget))]
|
||||
public class ChildController : MonoBehaviour
|
||||
{
|
||||
public enum Player { One, Two, Three, Four }
|
||||
@ -10,6 +11,7 @@ public class ChildController : MonoBehaviour
|
||||
public Player PlayerNumber;
|
||||
|
||||
private Child _child;
|
||||
private AutoTarget _autoTarget;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
@ -18,12 +20,15 @@ public class ChildController : MonoBehaviour
|
||||
InputManager.Instance.AddCallback((int)PlayerNumber, HandlePlayerButtons);
|
||||
|
||||
_child = GetComponent<Child>();
|
||||
_autoTarget = GetComponent<AutoTarget>();
|
||||
}
|
||||
|
||||
private void HandlePlayerAxis(MappedInput input)
|
||||
{
|
||||
if (this == null) return;
|
||||
|
||||
// movement
|
||||
|
||||
float xValue = 0f;
|
||||
|
||||
if (input.Ranges.ContainsKey("MoveLeft"))
|
||||
@ -47,6 +52,32 @@ public class ChildController : MonoBehaviour
|
||||
}
|
||||
|
||||
_child.Move(xValue, zValue);
|
||||
|
||||
// targeting
|
||||
|
||||
float xLookingValue = 0f;
|
||||
|
||||
if (input.Ranges.ContainsKey("LookLeft")) {
|
||||
xLookingValue = -input.Ranges["LookLeft"];
|
||||
}
|
||||
else if (input.Ranges.ContainsKey("LookRight")) {
|
||||
xLookingValue = input.Ranges["LookRight"];
|
||||
}
|
||||
|
||||
float zLookingValue = 0f;
|
||||
|
||||
if (input.Ranges.ContainsKey("LookForward")) {
|
||||
zLookingValue = input.Ranges["LookForward"];
|
||||
}
|
||||
else if (input.Ranges.ContainsKey("LookBackward")) {
|
||||
zLookingValue = -input.Ranges["LookBackward"];
|
||||
}
|
||||
|
||||
if (xLookingValue != 0 || zLookingValue != 0) {
|
||||
//transform.rotation = new Quaternion(0, 1, 0, Mathf.Atan2(zLookingValue, xLookingValue));
|
||||
transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Atan2(xLookingValue, zLookingValue) * Mathf.Rad2Deg, transform.eulerAngles.z);
|
||||
_child.target = _autoTarget.GetTarget(new Vector3(xLookingValue, 0, zLookingValue));
|
||||
}
|
||||
}
|
||||
|
||||
private void HandlePlayerButtons(MappedInput input)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user