I’m writing a lockon script with a first person controller, however it doesn’t work, as transform.forward just faces level to the xz plane. Is there a Vector3 option that takes into account the direction you are looking vertically as well?
My current code is:
using UnityEngine;
using System.Collections;
public class lockOnRockOn : MonoBehaviour {
public GameObject lockOn;
public GameObject samusControl;
public float lockOnDistance = 100.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.LeftShift)) {
Ray originRay = new Ray(transform.position, samusControl.transform.forward);
RaycastHit hitProperties;
Debug.DrawRay(samusControl.transform.position, samusControl.transform.forward, Color.green, 10.0f);
if(Physics.Raycast(originRay, out hitProperties, lockOnDistance)) {
if(hitProperties.transform.gameObject.tag == "enemy") {
lockOn = hitProperties.transform.gameObject;
}
}
samusControl.transform.LookAt (lockOn.transform);
}
}
}