diff --git a/Assets/Scenes/jp.unity b/Assets/Scenes/jp.unity index d987175..57c5bd1 100644 Binary files a/Assets/Scenes/jp.unity and b/Assets/Scenes/jp.unity differ diff --git a/Assets/Scripts/AutoTarget.cs b/Assets/Scripts/AutoTarget.cs new file mode 100644 index 0000000..13da159 --- /dev/null +++ b/Assets/Scripts/AutoTarget.cs @@ -0,0 +1,52 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; + +public class AutoTarget : MonoBehaviour { + + private List targets; + public float minAngleRange = 60f; + + // Use this for initialization + void Start () { + targets = new List(); + 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; + } + +} diff --git a/Assets/Scripts/AutoTarget.cs.meta b/Assets/Scripts/AutoTarget.cs.meta new file mode 100644 index 0000000..1c86e6a --- /dev/null +++ b/Assets/Scripts/AutoTarget.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 567fe96dcacb08f4ab716a98ce99fc88 +timeCreated: 1440221488 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Child.cs b/Assets/Scripts/Child.cs index 9342307..9f92da9 100644 --- a/Assets/Scripts/Child.cs +++ b/Assets/Scripts/Child.cs @@ -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); } } diff --git a/Assets/Scripts/ChildController.cs b/Assets/Scripts/ChildController.cs index 54826bf..e555200 100644 --- a/Assets/Scripts/ChildController.cs +++ b/Assets/Scripts/ChildController.cs @@ -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(); + _autoTarget = GetComponent(); } 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)