- Home /
Camera interfering with character movement while locking on to enemy.
Hello, I did my research first and have a script to store enemies tagged enemy adjusted it to my needs using Quaternion rotation to target the enemy. I have two problems that I can not figure out, my camera keeps on look at the enemies feet and player is being effected by my camera movement. Things I have commented out are stuff that did not work for me in the past I keep it there for a reminder.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class debug : MonoBehaviour
{
public List<Transform> targets;
public Transform selectedTarget;
public string targetTag = "Enemy";
private Transform myTransform;
//Use this for initialization
void Start()
{
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
AddAllEnemies();
}
public void AddAllEnemies()
{
GameObject[] go = GameObject.FindGameObjectsWithTag(targetTag);
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 SortTargetsByDistance()
{
targets.RemoveAll(target => target == null);
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;
}
selectedTarget = targets[index];
}
}
//Update is called once per frame
void Update()
{
/* if (Input.GetKeyUp(KeyCode.Tab))
{
TargetEnemy();
transform.LookAt(selectedTarget);
}
if (selectedTarget != null)
{
transform.LookAt(selectedTarget);
}
if (Input.GetKeyUp(KeyCode.Q))
{
selectedTarget = null;
return;
}
*/
if (Input.GetKeyUp(KeyCode.Tab))
{
TargetEnemy();
//transform.LookAt(selectedTarget);;
}
// lockOn();
}
void LateUpdate()
{
lockOn();
}
void lockOn()
{
if (Input.GetKeyUp(KeyCode.Tab))
{
//does not work TargetEnemy();
// this works but looks at feet transform.LookAt(selectedTarget);
Vector3 relativePos = selectedTarget.position - transform.position;
Vector3 myVector;
myVector = new Vector3(0, 10, 0);
// the second argument, upwards, defaults to Vector3.up
Quaternion rotation = Quaternion.LookRotation(relativePos, myVector);
transform.rotation = rotation;
}
if (selectedTarget != null)
{
// this works but looks at feet transform.LookAt(selectedTarget);
Vector3 relativePos = selectedTarget.position - transform.position;
Vector3 myVector;
myVector = new Vector3(0, 10, 0);
// the second argument, upwards, defaults to Vector3.up
Quaternion rotation = Quaternion.LookRotation(relativePos, myVector);
transform.rotation = rotation;
}
if (Input.GetKeyUp(KeyCode.Q))
{
selectedTarget = null;
return;
}
}
}
Answer by jbMcFly · Nov 06, 2018 at 03:52 AM
This code below fixed the problem with staring at the enemies feet by making a vector3 called myVector and making it equal to new Vector3(0, 1.5f, 0); and adding that to the transform of selectedTarget. Hopefully someone can help me with my camera problem I believe it has to do with the fact that im using the Unity standard player controller and it is not passing the update how I want.
void lockOn()
{
if (Input.GetKeyUp(KeyCode.Tab))
{
//does not work TargetEnemy();
// this works but looks at feet transform.LookAt(selectedTarget);
Vector3 myVector;
myVector = new Vector3(0, 1.5f, 0);
Vector3 relativePos = selectedTarget.position - transform.position + myVector;
// the second argument, upwards, defaults to Vector3.up
Quaternion rotation = Quaternion.LookRotation(relativePos, myVector);
transform.rotation = rotation;
}
if (selectedTarget != null)
{
// this works but looks at feet transform.LookAt(selectedTarget);
Vector3 myVector;
myVector = new Vector3(0, 1.5f, 0);
Vector3 relativePos = selectedTarget.position - transform.position + myVector;
// the second argument, upwards, defaults to Vector3.up
Quaternion rotation = Quaternion.LookRotation(relativePos, myVector);
transform.rotation = rotation;
}
if (Input.GetKeyUp(KeyCode.Q))
{
selectedTarget = null;
return;
}
}
$$anonymous$$y question now is there a way of storing multiple enemy heights while all under the tag enemy? and how would I call them?