Conflicts:
	Assets/Prefabs/Child.prefab
	Assets/Scripts/Child.cs
This commit is contained in:
Patrice Vignola 2015-08-22 05:34:52 -04:00
commit c0f938a59a
6 changed files with 97 additions and 1 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,47 @@
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;
}
}
return closest;
}
}

View 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:

View File

@ -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);
}
}

View File

@ -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<Child>();
_autoTarget = GetComponent<AutoTarget>();
_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)