Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Persona · Nov 02, 2010 at 04:36 PM · movementlookatcancellockon

Movement while Looking At an enemy

function Update(){ var hit : RaycastHit; var fwd = transform.TransformDirection(Vector3.forward);

 if (Physics.Raycast (transform.position, fwd, 100)) {
     if (target.gameObject.tag == "Enemy"){
     transform.LookAt(target);
     }
 }

}

I have this script that allows my character to Lock on to an enemy he's facing but he can no longer move left or right and can't rotate as long as he's within range. So I need to ask for 2 things:

1.) A method of free movement while locking onto the target.

2.) A method of breaking the raycast via button press (Disable the lock on feature)

Any help will be appreciated.

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
1
Best Answer

Answer by Atnas1010 · Nov 02, 2010 at 05:33 PM

1) I don't see why the character can't move left or right, while he is locked on. As for the rotate part, that is because you force him to look at the target, so naturally he cant turn.

2) You could edit your script line to this (you would have to declare toggle in the beginning of the script)

if (Input.GetKeyDown(KeyCode.Space))
    toggle = !toggle;
if (toggle && Physics.Raycast (transform.position, fwd,100)) {

Does this make it work like you want it to?

Comment
Add comment · Show 19 · 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
avatar image Persona · Nov 02, 2010 at 07:51 PM 0
Share

$$anonymous$$eyCode doesn't go there, a string does, but even with those corrects it doesn't seem to.

As for the rotate, I meant it moves around the locked on enemy, but that was my miswording.

avatar image Atnas1010 · Nov 02, 2010 at 08:00 PM 0
Share

Are you looking something the user should hold down all the time to keep the raycast disengaged, or like a toggle raycast? (I wrote that code to do the first option, but I see now the error. Try with GetButton ins$$anonymous$$d of GetButtonDown. Now that you have explained yourself a little better, I don't understand question 1)

avatar image Persona · Nov 03, 2010 at 02:14 AM 0
Share

Thee ray cast is always on. I need a way to break it for awhile....on second thought, let's go with the toggle. Set up a Boolean to compare?

avatar image Atnas1010 · Nov 03, 2010 at 02:28 AM 0
Share

Yup, a boolean is always nice :) Edited my post to show how I'd do it.

avatar image Persona · Nov 05, 2010 at 01:25 AM 0
Share

No luck with it yet.

Show more comments

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

No one has followed this question yet.

Related Questions

Rotate character to the moving direction problems? 2 Answers

How do I get GameObjects to look at me? 2 Answers

Moving a wheel character 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Look Where You're Going 3 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