Ok so this is pretty weird to me. I am trying to make a lock on function that turns the player toward the enemy after the lock on key is pressed. This works fine. I use targetRotation when rotating normally which is based on mouse movements, and when rotating the player while locked on to the enemy so that the player always faces the enemy. The problem I am having is that when you disable lock on it resets targetRotation back to (0,0,0,1) so it turns the player to its initial rotation, and I want it to just disable and continue facing in w/e direction it was facing while locked on.
I tried updating the angles I use when locked on but that doesn’t do anything. If I need to post smaller snippets please say so, but for now I am just going to post the whole script because it isn’t that long.
using UnityEngine;
using System.Collections;
public class PlayerControl2 : MonoBehaviour {
// initial speed for all movement
public float speed = 10;
// initial speed for all turning
public float turnSpeed = 6;
// replacing the jump function with this to achieve dashing
public float dashDist = 5.0f;
// delay input to make sure player wanted to make that action
public float inputDelay = 0.1f;
// bools to use in FixedUpdate
private bool wPressed;
private bool qPressed;
private bool ePressed;
private bool sPressed;
// rotation for the player
public float horizontalAimingSpeed = 400f;
public float verticalAimingSpeed = 400f;
private float angleH, angleV = 0;
private Quaternion targetRotation;
// Lock on
public bool lockOn = false;
public GameObject lockIcon;
GameObject closestEnemy;
// cache the rigidbody that will be used to move object
public Rigidbody rb;
public float turnInput;
// Use this for initialization
void Start () {
//targetRotation = transform.rotation;
if (GetComponent<Rigidbody> ())
rb = GetComponent<Rigidbody> ();
else
Debug.LogError ("Need rigidbody");
turnInput = 0;
}
// Update is called once per frame
void Update () {
// triggers the lock on ability
if (Input.GetKeyDown (KeyCode.C))
lockOn = !lockOn;
if (lockOn) {
// right now finds closest enemy of everyone in scene want to modify to have a range
closestEnemy = FindClosestEnemy ();
// sets lock icon to closest enemy to player
lockIcon.transform.position = new Vector3(closestEnemy.transform.position.x, closestEnemy.transform.position.y, closestEnemy.transform.position.z);
lockIcon.SetActive (true); // turns the object connected to lockIcon on
// direction of lock on
Vector3 relPos = lockIcon.transform.position - transform.position;
// Sets a rotation for the player to look in
targetRotation = Quaternion.LookRotation (relPos, Vector3.up);
Debug.Log ("targetRotation = " + targetRotation + " when locked on");
// rotates the player in the direction of the lock icon
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed* Time.deltaTime);;
angleH = transform.rotation.y;
angleV = transform.rotation.x;
turnInput = Input.GetAxis ("Horizontal"); // turns player left or right in place
// method that moves player left and right
Strafe ();
// sets the key strokes to be used in FixedUpdate
if (Input.GetKey (KeyCode.W))
wPressed = true;
else
wPressed = false;
if (Input.GetKey (KeyCode.E))
ePressed = true;
else
ePressed = false;
if (Input.GetKey (KeyCode.Q))
qPressed = true;
else
qPressed = false;
if (Input.GetKey (KeyCode.S))
sPressed = true;
else
sPressed = false;
} else {
// Turns lockIcon off when the player disables lock on
lockIcon.SetActive (false);
turnInput = Input.GetAxis ("Horizontal"); // turns player left or right in place
// sets angle of rotation with mouse movement
angleH += Mathf.Clamp (Input.GetAxis ("Mouse X"), -1, 1) * horizontalAimingSpeed * Time.deltaTime;
angleV += Mathf.Clamp (Input.GetAxis ("Mouse Y"), -1, 1) * verticalAimingSpeed * Time.deltaTime;
// method that moves player left and right
Strafe ();
// method that handles mouse rotations
Turn ();
// sets the key strokes to be used in FixedUpdate
if (Input.GetKey (KeyCode.W))
wPressed = true;
else
wPressed = false;
if (Input.GetKey (KeyCode.E))
ePressed = true;
else
ePressed = false;
if (Input.GetKey (KeyCode.Q))
qPressed = true;
else
qPressed = false;
if (Input.GetKey (KeyCode.S))
sPressed = true;
else
sPressed = false;
}
}
// FixedUpdate called for ohysics based updates
void FixedUpdate(){
if (wPressed)
rb.velocity = transform.forward * speed; // player moves forward
if (ePressed) // e and q move player up and down respectively
VerticalTurn ();
if(qPressed)
VerticalTurn ();
if (sPressed)
rb.velocity = -transform.forward * speed; // player moves backward
}
void Strafe(){
if (Mathf.Abs (turnInput) > inputDelay) {
// moves player left or right based on input
// turnInput will always be between -1 and 1
transform.Translate(Vector3.right * turnInput * speed * Time.deltaTime);
}
}
void Turn(){ // try to modify for a smoother rotation.
// sets rotation based on mouse movements
targetRotation = Quaternion.Euler (-angleV, angleH, 0);
Debug.Log ("targetRotation = " + targetRotation + " when not locked on");
// sets players rotation
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed* Time.deltaTime);
}
void VerticalTurn(){
if (ePressed) // moves the player up
transform.Translate(Vector3.up * 1.0f * speed * Time.deltaTime);
else if (qPressed) // moves the player down
transform.Translate(Vector3.up * -1.0f * speed * Time.deltaTime);
else if (ePressed && qPressed) { // supposed to do nothing but needs to be tested
}
}
// returns the closest object with the tag of Enemy in the whole scene
GameObject FindClosestEnemy() {
// creates array to store objects with the tag Enemy
GameObject[] gos;
// searches all objects in the scene for the tag Enemy
gos = GameObject.FindGameObjectsWithTag("Enemy");
// return object as a GameObject
GameObject closest = null;
// sets the range you want to use to check around the player
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos) {
// returns distance between player's position and enemy's position in (x,y,z) format
Vector3 diff = go.transform.position - position;
// converts it to a distance as a float
float curDistance = diff.sqrMagnitude;
// if enemy is within range set them as the closest enemy and make the
// distance between the player and that enemy the new range
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
}