Character continues moving when locked onto an enemy
Ok so here's my issue, I have a script that allows me to lock on to and orbit an enemy target. The problem is if my character is moving when pressing the lock on button the character will continue moving in whatever direction it was going before the button was pressed regardless of any more button presses save for the opposite direction.
This doesn't happen when I'm stationary, if my character stands still and I press the lock on button everything works fine. It's only when I'm moving.
I'm not sure if this is enough information, or what else someone might need to better understand but here is my script.
if (GetComponent<DetectedEnemies>().locked)//checks bool to see if lockOn button was pressed
{
mainCam.SetActive(false);//turns off main cam
lockCam.SetActive(true);//turns on lockOn cam
movesetting.tarObject = GetComponent<DetectedEnemies>().Closest;//target object will be the closest enemy to player
this.transform.LookAt(movesetting.tarObject.transform.position);//player will look at the target object at all times
if (currXRot.x > maxXRot.x)//if the current x rot of this object exceeds the maxXRot it can go...
currXRot.x = maxXRot.x;//it gets reset
transform.Translate(Vector3.right * movesetting.speed * Time.deltaTime * turnInput);//orbits around the target object
transform.Translate(Vector3.forward * movesetting.speed * Time.deltaTime * forwardInput);//in control of moving towards and away from target
}
Any and all help is greatly appreaciated, thank you in advance. If you need more info or if something isn't clear please let me know.
Answer by mirajose93 · Mar 19, 2016 at 09:01 PM
Found a solution, not listed in this part of the script I use a variable called velocity to add a force in a direction of the rigidbody. So I went back and used the force applied by the velocity variable to move the player forward and back but I kept the sideways motion the same, since it wasn't acting funky anymore so now it looks like:
transform.Translate(Vector3.right movesetting.speed Time.deltaTime * turnInput); velocity.z = movesetting.forwardVel*forwardInput;
ForwardVel is a variable for how much force is applied, and forwardInput is the Vertical input found by defaualt with unity's Input Manager. This is all a modified version of the original script from Renaissance Coders, https://www.youtube.com/watch?v=BBS2nIKzmbw&list=PL4CCSwmU04MjDqjY_gdroxHe85Ex5Q6Dy∈dex=1
Your answer
Follow this Question
Related Questions
Spawned enemy walk from right to left destroying turrets. 0 Answers
Change Camera's size relative to player's speed 0 Answers
Top Down Character - Face direction of movement? 0 Answers
Object Player Movement stops working after Restarting Unity 0 Answers
How to move 3 players at the same time in V form,How to move 3 characters in the same time in V form 0 Answers