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 TritZium · Jul 14, 2014 at 09:51 PM · c#aimagnitudecomparison

Why is this not giving me an offset

Hello!

I'm currently trying to make a simple AI for a spaceflight game, and I want to "cap" how close the AI can be to the player, I made a script using the transform.position.magnitudes to make sure they didn't get closer then the desired offset. However it aint working and I have absolutely no idea of why.

             if(-offsetfromplayer <= (enemytransform.magnitude - this.gameObject.transform.position.magnitude) && (enemytransform.magnitude - this.gameObject.transform.position.magnitude) < 0f){
 
 movementspeed = 0f;
 
 localvelocity = new Vector3(0f,0f, movementspeed);
             }

offsetfromplayer is 50 by the way. Which means that -offsetfromplayer is -50.

As you can see this is when the magnitude difference is negative, and it is negative when I do the tests. I know that because I've got the magnitude differences show up in the inspector. So that is updating correctly, -offsetfromplayer also show up correctly in the console. And that should work as long as the magnitude differences is between -50 and 0. Right?

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

Answer by robertbu · Jul 15, 2014 at 04:49 AM

The code you have is a bit confusing. magnitude is a property of a Vector3, not a transform, but you call your variable 'enemytransform'. If it really is a Transform, you should be getting a compiler error.

But from a logic standpoint, this code is also wrong. Let's assume that 'enemtransform' is a Vector3. If so, you find the distance between the two objects by:

 float dist = (transform.position - enemytranform).magnitude;

So your code would be:

 if ((transform.position - enemytransform).magnitude < offsetfromplayer) {
     movementspeed = 0.0f;
     lovalvelocity = Vector3.zero;
 }

 
Comment
Add comment · Show 1 · 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 TritZium · Jul 16, 2014 at 12:10 PM 0
Share

enemytransform is initialized(Updated?) in the beggining of FixedUpdate.

After some more looking around I found Vector3.Distance which is what I use now ins$$anonymous$$d. But thanks anyway!

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

2 People are following this question.

avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Android back button always quits app. 1 Answer

EnemyAI C# Error 1 Answer

Rigidbody (Player) goes through wall 1 Answer


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