- Home /
Locking onto enemies with the third person controller
Hi there. I've got a simple scene setup with a player using the third person controller from the 3D Platformer tutorial and an enemy. I've got the code found at the bottom here in a function that a I call in LateUpdate with GetButton so that while I hold the button down, the player "locks on" and continuously faces the enemy while moving around him not unlike various 3D action games like Phantasy Star Online, Devil May Cry, etc.
function Update () { var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("waypoint"); var closest: GameObject; var closestDist = Mathf.Infinity;
for (waypoint in waypoints) {
var dist = (transform.position - waypoint.transform.position).sqrMagnitude;
if (dist < closestDist) {
closestDist = dist;
closest = waypoint;
}
}
transform.LookAt(closest.transform);
}
This works almost perfectly. The problem is, when you let go of the button, the player's facing immediately snaps back to the direction you were moving in. So, for example, if you were locked on and moved away from the enemy then let go of the button, the player's facing immediately snaps away from the enemy. I'm assuming this is because LookAt isn't affecting what the Third Person Controller's using to rotate the character's facing in the direction its moving (moveDirection?), however, I'm unsure as to how to use the enemy's position with the movement direction so that when you let go of the button, the player still faces the enemy at that moment until you move him in a different direction. I'd appreciate any help I could get!
Answer by AnaRhisT · Jun 22, 2010 at 09:02 AM
while(Vector3.Distance(enemy.position, transform.position) > 0.1){
var direction : Vector3 = transform.position - enemy.position;
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), 0.2* Time.deltaTime);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
yield;
}
You can change the while loop to something like while(Enemy is visible on the camera) or whatever. The code above will give u a basis to what u need.
Hi, thanks for responding. I put the code inside the function I call in LateUpdate and the problem still persists. The player immediately faces the direction you were last moving in.
Just wanted to update that this was indeed the basis for what I needed. I put this in my function and then used GetButtonUp to add a tiny invisible amount of movement in the direction of the enemy in Update before UpdateSmoothed$$anonymous$$ovementDirection is called. This keeps the player facing in the direction of the enemy after you stop locking onto them. There's a some amount of unwanted rotation on the x and z axis that happens as you lock on and off that goes away immediately while moving and does cause a bit of strange behavior, but I'll just have to iron that out. Thanks a ton!
Answer by taib · Jul 18, 2020 at 05:00 PM
after 10 years i reply, just use bool like.. if (input.getbuttondown ("fire1") { isLocked = true; } dont know if this question still available or not.. sory for my bad English ahaha
Your answer
Follow this Question
Related Questions
How can I make my gameObject find the nearest object with a specific tag and rotate to look at it? 6 Answers
Problem with the direction of the raycast when rotating 1 Answer
rotate to swip direction 0 Answers
Rotation is jumpy 2 Answers
C# Rotate GameObject at Other Point other than Center 3 Answers