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 /
avatar image
0
Question by fonql · Jan 04, 2016 at 09:21 PM · positionvector3directionmathcalculation

Calculate a new position instead of using Vector3.back

I am writing a simple avoidance function. Spheres (enemies) target and move towards a capsule (player) to hinder its movement. When a sphere hits the capsule I instruct it to jump back in its own local space, after which it resumes moving towards the enemy again:
enemy.transform.Translate(Vector3.back);

After the jump I check whether its new position might overlap with another enemy, if so, I move that enemy backwards in it's local space as well.
Now instead of translating multiple enemies I'd prefer to calculate an enemy's new position to see if it interferes with another enemy position.

The trouble I have is that the enemy has it's own local rotation and I do not know how to calculate its new position after a vector3.back translation that takes in account its local rotation (or maybe direction, actually).
I simply do not know and cannot find the math to accomplish this.

So ehm,.. Help? Thanks in advance!

Remi

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

Answer by Dave-Carlile · Jan 04, 2016 at 09:45 PM

transform.forward gives you the object's direction in world space. You can use -transform.forward for the reverse direction.

That said, is there a reason you don't just use the built-in physics? That should be able to resolve this sort of thing.

Comment
Add comment · Show 7 · 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 fonql · Jan 05, 2016 at 02:28 PM 0
Share

Hey Dave,

$$anonymous$$any thanks for your reply!
I did not realize transform had a .forward method (found it in the docs now though).
Neither Did I know you could use -($$anonymous$$us)transform.
This looks like something that can be very useful, going to try it out asap.

As for the physics, i'm targeting html5 so if I can omit the physics library it will be a smaller download. I'm only using spheres, so simple distance calculations are only needed for overlapping stuff.

$$anonymous$$any thanks again!

avatar image Bonfire-Boy fonql · Jan 05, 2016 at 02:31 PM 1
Share

It's not -(transform) it's -(transform.forward). In other words it's a vector that's being negated, not a Transform.

avatar image fonql · Jan 05, 2016 at 04:43 PM 0
Share

Been working with this, but somehow it looks as if the new enemy position is not in the position I expect it to be. $$anonymous$$y code:

 public void EnemyAvoidance (GameObject avoidObject)
     {
         Vector3 temppos = avoidObject.transform.position;
         temppos +=-(transform.forward);
         print (temppos);
         Debug.Break ();
     }

alt text

The top of the image is the situation when an enemy(white sphere) touches the player(green sphere), including the new calculated position in the console. After above code I expect the enemy to be below its current position but in line with the purple arrow (p).
However, when I enter the console output in the inspector manually the enemy ends up in a different position.
Not quite sure what to make of this.. Any help much appreciated! Thanks.

v3uitleg.png (16.8 kB)
avatar image Bonfire-Boy fonql · Jan 05, 2016 at 04:51 PM 0
Share

Am I right in thinking that the white sphere is the "avoidObject" and the green sphere is the gameObject which has the EnemyAvoidance function's script on it?

If so, can you explain where you get the purple line's direction from? It looks like it's calculated relative to the white sphere's rotation. But if my first para is correct then you would expect the white sphere to move in a direction deter$$anonymous$$ed by the green object's rotation (because you're using transform.forward rather than avoidObject.transform.forward).

avatar image fonql Bonfire-Boy · Jan 05, 2016 at 05:03 PM 0
Share

The white object is indeed the "avoidObject". EnemyAvoidance however is just a class with a default transform of 0,0,0;0,0,0;1,1,1
So yeah I should use avoidObject.transform.forward, but unity wont let me input avoidObject.-(transform.forward); Guess I have the syntax wrong?

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

41 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers

how to find direction between two points 2 Answers

Finding difference between gameObject's x coordinate according to one object's transform 1 Answer

How to rotate an object to face the direction it's going? 1 Answer

Creating a bouncing game without using physics - Vector3 math problem, 2 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