Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by mirajose93 · Mar 19, 2016 at 08:55 AM · movementplayerenemyorbittargeting

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges