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 Triqy · Apr 14, 2013 at 08:47 PM · instantiateailookatwaypoint

I need to instantiate a empty gameObject 2 Units right/left of a 'Enemies' Transform?

Hello, I need to instantiate a empty gameObject/Waypoint 2 Units right/left of a 'Enemies' Transform? I am making the enemies AI and need to drop a Waypoint either right or left of the enemy so if the enemy collids with a say a rock the enemy looks at the newly created Waypoint and goes to that Waypoint and then the Waypoint destories itself when triggered. The enemy then looks at the main object that it was trying to get to get to like in RTS Games... I hope that makes sense :P

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Triqy · Apr 17, 2013 at 06:11 PM

i figured it out myself. This is what I used!

 var CreateWaypoint = false;
 
 function Upadate(){
 
 if(CreateWaypoint == true){
     var CorrectionWay = instantiate(CorrectionWaypoint, Vector3(transform.localPosition * 2.3f, transform.localPosition.y, transform.localPosition.z), transform.rotation); 
     
     CorrectionWay.name = "WayPoint";
 CreateWaypoint = false;
 
 }
 }

This creates a waypoint about 2 units to the left of the enemy

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
avatar image
0

Answer by 1337GameDev · Apr 14, 2013 at 09:35 PM

Well, to get the position left or right, just find the point left or right of where your enemy is. Here is some untested, top of my head, code ( c# ):

 Transform enemyTransform; //set this to the enemy, however you want
 Transform newTransform = enemyTransform;
 float amount = 2.0f; //amount to check
 
 newTransform.Translate(Vector3.right * amount); //move right
 Instantiate(wayPointPrefab, newTransform.position, Quaternion.zero);
 //this assumes a prefab has been loaded into wayPointPrefab, use Resources.Load for this or inspector variable
 //Quaternion.zero rotation orients to world zero rotation, if you need something different use that, you could also use enemyTransform.rotation

I believe this will work fine. There might be a quicker way to do this with directly modifying x,y, or z components of newTransform.position, but this was the first way I thought of.

Comment
Add comment · Show 3 · 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 1337GameDev · Apr 14, 2013 at 09:36 PM 0
Share

Remember this is untested, so there might be errors, but this should show the general methodology

avatar image Triqy · Apr 14, 2013 at 11:47 PM 0
Share

Well I was planning on having the enemy turn towards the Waypoint and then when the enemy hits the Way turn back to the original destination using transform.LookAt(); (JS)

I can read C# but read/write in JS

The Quaternion.Zero? Will that trun the enemy toward the newly created Way or is that fpr the instainated prefab? Thanks

avatar image 1337GameDev · Apr 15, 2013 at 05:51 AM 0
Share

Quaterion.zero is just to give an all zero rotation to the instantiated prefab. You can change this to your desired direction to save some instructions.

Look at Aron greenberg's a star path finding project. It has a lot of path finding tools you could make use of. There is a free and paid version.

I would highly suggest learning c#, as its more similar to other program$$anonymous$$g languages and JS is unity specific. C# is also used outside of unity a lot, so you can find scripting help from non unity places. JS is also a sore to look at, atleast for me.

avatar image
0

Answer by Owen-Reynolds · Apr 15, 2013 at 01:00 AM

When you say right or left, it usually means you're thinking in local coordinates (select a rotated object in the editor and toggle Local/Global at top-left to see the difference.) Your right, of the way you are facing now.

To move two units right (your right, local): Vector3 p2 = transform.position + transform.right*2;. Trans.right is essentially the little Red direction arrow when you're moving an object.

To compare, world movement (east, in this case) is transform.position + Vector3.right*2;

If you're lying on your side, transform.right could be up or down, but a typical "mob" is always head-up, so transform.L/R/F/B is always flat along the ground.

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 1337GameDev · Apr 15, 2013 at 05:54 AM 0
Share

Yup. This seems correct at first glance.

Also, align your model so that the forward direction actually is forward. If you don't, his code will have your object calculate wrong movement (as right, left, forward and backward are relative to how the model is oriented).

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

13 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

Related Questions

how to detect if transform.lookat is going through walls? 1 Answer

Simulating Mouse Input for AI 0 Answers

Follow up to AI Pathfinding Question 0 Answers

Any ideas on a simple AI script that doesn't use any drag/drop variables 1 Answer

Random Running and Follow at Certain Distance 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