diff --git a/Assets/Prefabs/Child.prefab b/Assets/Prefabs/Child.prefab index 80ded23..016e3b7 100644 Binary files a/Assets/Prefabs/Child.prefab and b/Assets/Prefabs/Child.prefab differ 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..d484f15 --- /dev/null +++ b/Assets/Scripts/AutoTarget.cs @@ -0,0 +1,47 @@ +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; + } + } + + 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 c58226f..ca953e2 100644 --- a/Assets/Scripts/Child.cs +++ b/Assets/Scripts/Child.cs @@ -15,6 +15,7 @@ public class Child : MonoBehaviour private float _xValue; private float _zValue; private bool _isSleeping; + public Transform target; private int _index; @@ -45,6 +46,11 @@ public class Child : MonoBehaviour Debug.Log("Player " + _index + " is being spotted by mom."); } + + // look at the target + if (target != null) { + transform.LookAt(target); + } } void OnTriggerEnter(Collider other) { @@ -55,7 +61,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 38849ba..fca1baa 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,6 +20,7 @@ public class ChildController : MonoBehaviour InputManager.Instance.AddCallback((int)PlayerNumber, HandlePlayerButtons); _child = GetComponent(); + _autoTarget = GetComponent(); _child.Index = (int)PlayerNumber; } @@ -25,6 +28,8 @@ public class ChildController : MonoBehaviour { if (this == null) return; + // movement + float xValue = 0f; if (input.Ranges.ContainsKey("MoveLeft")) @@ -48,6 +53,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)