Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Camera script won't focus on targeted enemy

$
0
0
I cant get my targeting script to tell my camera script to go from the player to the enemy targeted. I also wanted to make it so when it focuses on the enemy it keeps my player in view at all times. Here is the Camera script: using UnityEngine; using System.Collections; public class HackAndSlashCamera : MonoBehaviour { public Transform target; public string playerTagName = "Player"; public float walkDistance = 4.0f; //the camera distance behind the player when the player is walking public float runDistance = 4.0f; //the camera distance behind the player when the player is running public float height = 20.0f; //the height we want the camera to be public float xSpeed = 25.0f; //the speed at which the camera will move when looking left to right public float ySpeed = 12.0f; //the speed at which the camera will move when looking up and down public float heightDamping = 2.0f; public float rotationDamping = 3.0f; public int yMinLimit = -80; public int yMaxLimit = 80; private Transform _myTransform; private float _x; private float _y; // private bool _camButtonDown = false; void Awake(){ _myTransform = transform; //cache our transform so we do not need to look it up all of the time // Screen.showCursor = false; //hides the mouse cursor } // Use this for initialization void Start () { //if we do not have a target, let them know, else set the camera up according to where our target is. if(target == null) Debug.LogWarning ("We do not have a target for the camera"); else { CameraSetUp(); } } void Update(){ _myTransform.position = target.position; // _myTransform.transform.rotation = Quaternion.Euler(new Vector3(90, 0, 0)); // 90 degress on the X axis - change appropriately } //this function is called after all of the Update functions are done void LateUpdate(){ GameObject go = GameObject.FindGameObjectWithTag(playerTagName); target = go.transform; // lets the mouse govern camera position // _x += Input.GetAxis("Mouse X") * xSpeed * 2.0f; // _y -= Input.GetAxis("Mouse Y") * ySpeed * 2.0f; _y = ClampAngle(_y, yMinLimit, yMaxLimit); // sets camera rotation Quaternion rotation = Quaternion.Euler(_y, _x, 0); Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position; _myTransform.rotation = rotation; _myTransform.position = position; // Calculate the current rotation angles float wantedRotationAngle = target.eulerAngles.y; float wantedHeight = target.position.y + height; float currentRotationAngle = transform.eulerAngles.y; float currentHeight = transform.position.y; // Damp the rotation around the y-axis currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); // Damp the height currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime); // Convert the angle into a rotation Quaternion currentRotation = Quaternion.Euler (currentHeight, currentRotationAngle, 0); // Set the height of the camera _myTransform.position = new Vector3(_myTransform.position.x, currentHeight, _myTransform.position.z); //limits where the mouse can look _x = ClampAngle (_x, -90, 90); _y = ClampAngle (_y, -2.5f, 35); } public void CameraSetUp(){ // calculate desired camera position _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance); // Always look at the target _myTransform.LookAt(target); } private 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); } } Here is the targeting script /// /// Targetting.cs /// Ameen Missoumi /// 3/3/2013 /// /// This script can be attached to any permanent gameobject and is responsible for allowing the player to target different mobs that are with in range /// using UnityEngine; using System.Collections; using System.Collections.Generic; public class Targeting : MonoBehaviour { public List targets; public Transform selectedTarget; public string enemyTagName = "Enemy"; private Transform myTransform; // Use this for initialization void Start () { targets = new List(); selectedTarget = null; myTransform = transform; AddAllEnemies(); } public void AddAllEnemies() { GameObject[] go = GameObject.FindGameObjectsWithTag(enemyTagName); foreach(GameObject enemy in go) AddTarget(enemy.transform); } public void AddTarget(Transform enemy) { targets.Add(enemy); } private void SortTargetsByDistance() { targets.Sort(delegate(Transform t1, Transform t2) { return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position)); }); } private void TargetEnemy() { if(selectedTarget == null){ SortTargetsByDistance(); selectedTarget = targets[0]; } else { int index = targets.IndexOf(selectedTarget); if(index < targets.Count - 1){ index++; } else { index = 0; } DeSelectTarget(); selectedTarget = targets[index]; } SelectTarget(); } private void SelectTarget(){ Transform name = selectedTarget.FindChild("Name"); if(name == null) { Debug.LogError("Could not find the Name on " + selectedTarget.name); return; } name.GetComponent().text = selectedTarget.GetComponent().Name; name.GetComponent().enabled = true; selectedTarget.GetComponent().DisplayHealth(); Messenger.Broadcast("show mob vitalbars", true); } private void DeSelectTarget(){ selectedTarget.FindChild("Name").GetComponent().enabled = false; selectedTarget = null; Messenger.Broadcast("show mob vitalbars", false); } // Update is called once per frame void Update () { if(Input.GetKeyDown(KeyCode.Tab)) { TargetEnemy (); } } void LateUpdate(){ targets = new List(); myTransform = transform; AddAllEnemies(); } }

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>