Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

2 players(not really) one camera, camera must prioritize one of them

$
0
0
So, I'm trying to make a script that put the camera smoothly with a lerp between 2 objects (or even 3) one player and one or more enemy. the problem is that if the get too far away form each other the camera will not focus the player and him should be on camera all time so i crated this: using System.Collections; public class Movimiento_camara : MonoBehaviour { public float vel_lerp = 1.6f; [Range(2, 10)] public float distancia_maxima = 6; private Vector3 vector_objetivo; private GameObject player; private GameObject[] enemigos; private int[] enemigos_pos_x; private float suma_valores_media; private float media; void Start () { player = GameObject.Find ("player"); enemigos = GameObject.FindGameObjectsWithTag ("enemy"); //enemigos_pos_x = GameObject.FindGameObjectsWithTag ("enemy")<>.transform.position.x; } void Update () { suma_valores_media = player.transform.position.x; if(enemigos.Length > 0) { for (int i = 0; i < enemigos.Length; i++) { suma_valores_media = suma_valores_media + enemigos [i].transform.position.x; } } media = suma_valores_media/(enemigos.Length+1); //this is the part that limit the position of the camera attaching it to the player if (player.transform.position.x - media > 0) { if (player.transform.position.x - media > distancia_maxima) { if (media > player.transform.position.x) { media = player.transform.position.x + distancia_maxima; } else { media = player.transform.position.x - distancia_maxima; } } } else { if (player.transform.position.x + media < -distancia_maxima) { if (media > player.transform.position.x) { media = player.transform.position.x + distancia_maxima; } else { media = player.transform.position.x - distancia_maxima; } } } Vector3 vector_objetivo = new Vector3(media, this.transform.position.y, this.transform.position.z); this.transform.position = Vector3.Lerp (this.transform.position, vector_objetivo, Time.deltaTime*vel_lerp); } } the problem is that the transion between the normal and smooth movement and the limited movement is so rought, and i would love to make it way smoother. sorry for potato english :P and thanks.

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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