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 · Jun 27, 2013 at 09:07 AM · instantiatebuildcreatez-axis

How do i instantiate along the z axis?

So my empty object that i have creates 10 wall segments 10 units apart... How do i make the created segments follow the z axis with the same rotation as the creator so that any rotation i put on to the creator, the wall segments will always follow the z axis in a straight line?

Basically if i rotate the creator and press a button, 10 wall segments will be created 10 units apart in a straight line any way i rotate the creator along the local z axis?

This is what i have so far, but i cant seem to make i follow the z axis in a local straight line... Ive tried a crazy amount of things and been stuck for awhile. Thanks for the help!

 #pragma strict
 //point B Marker
 var target: Transform;
 public var Wall: Transform;
 
 var create = false;
 
 function Start () {
 
 }
 
 function Update () {
 
 if(Input.GetKeyUp("space")){
 
 create = true;
 
 }
 
 transform.LookAt(target);
 
 var relativePos = target.position - transform.position;
 var Distance = Vector3.Distance(target.position, transform.position);
 var ModelsNeeded = Distance / 10;
 var Fwd = transform.TransformDirection(Vector3.forward);
 
 print("Distance to other: " + Distance);
 
 print("Models Needed for Distance: " + ModelsNeeded);
 
 
 if(create == true){
 
 for (var i : int = 0;i < ModelsNeeded; i++) {
 
 Instantiate (Wall, Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z  + (i * 10)), Quaternion.LookRotation(relativePos));
  
 create = false;
  
 }
 }
 }


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

2 Replies

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

Answer by robertbu · Jun 28, 2013 at 12:37 AM

I'm assuming you want the local 'z' of the object, not the world 'z' axis. It will always be transform.forward. To calculate the position you want:

 var v3Pos = transform.position + transform.forward * (i * 10);
 Instantiate (Wall, v3Pos, transform.rotation);
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 Triqy · Jun 28, 2013 at 04:45 AM 0
Share

Thank you so much man! I had var in my script that had transform.forward i just didnt know how to set it up correctly. you just solved a 3 day issue of $$anonymous$$e.. as sad as that sounds. Thanks ALOT lol

avatar image
0

Answer by mohitramani · Jun 27, 2013 at 10:35 AM

I didnt understand your question completely but I guess the following should help you.

lets say you have a prefab named Cube and a clone of that cube is placed in the scene which will be your creator to which you can apply rotation(using some script of yours) and then press some key to create N clones behind it.

public GameObject prefab;

 void Start () {
 
 }
 
 void Update () {
     if(Input.GetKey("SomeKey"))
     {
         for(int i=0;i<N;i++)
         {
             Instantiate(prefab,new Vector3(transform.position.x,transform.position.y,transform.position.z + (i*10)),transform.rotation);    
         }
     }        
 }
Comment
Add comment · Show 6 · 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 Triqy · Jun 27, 2013 at 11:17 AM 0
Share

You didn't change a thing... except the rotation at the end and i have already tried that as well. And why did you delete some stuff? I don't understand your answer.

Im not trying to rotate my creator, but rather create wall segments down the z axis. Say you have the z axis right.. when i press space i want to create wall segments down the z-axis between 2 point in 3d space.

say

point A is at x= 0, y=0 , z=0,

point b is at x= 10, y=0, z=10

I want to create wall segments down a straight diagonal line that from point A TO point B that all have the save local rotation.

avatar image Triqy · Jun 27, 2013 at 11:19 AM 0
Share

Btw i code in both c# and javascript

avatar image Triqy · Jun 27, 2013 at 11:02 PM 0
Share

Bump! Please help ive been stuck for a couple of days now thanks

avatar image Hoeloe · Jun 27, 2013 at 11:37 PM 0
Share

You never actually said what the problem was, but I think I can guess. You're instantiating the object using the localPosition, but the instantiation occurs in global space, so it's likely that everything is working, just in the wrong place.

avatar image Triqy · Jun 27, 2013 at 11:50 PM 0
Share

Exactly! I probably explaned it wrong. I need all the wall segments to be locals of the creator that run down along with the z-axis of the creator in a straight line!

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

17 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

Related Questions

[Closed] Instantiate wall/fence down a raycast from point A to point B? 0 Answers

Distribute terrain in zones 3 Answers

The problem with FindObjectOfType and Instantiate.Why is not one object created, but a multiplied copy? 1 Answer

Scripting errors only when building for iPhone 2 Answers

Any way to create a gamobject only if there is an empty space? 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