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 newbinaryfile · Nov 13, 2013 at 01:20 AM · rotationinstantiatequaternion

Instantiate between 2 game objects.

Hi,

I am trying to instantiate cube prefab between 2 objects.I am drawing lines with vectorsity and when i completed shape my script trying to instantiate object between 2 points.alt text

My lines using vector3 and also i am working on X / Y axis. when i instantiate prefab between 2 points i have to rotate them in Z axis. But when i tried to instantiate an object there is no rotations applied.

alt text

That black line is my cube on isometric cam view and i want to rotate them between 2 vector3 points. I need some advices for calculating rotation on z axis.

How can i do that ?

Sorry for my bad english

Best Regards and Thanks

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
1

Answer by rutter · Nov 13, 2013 at 01:30 AM

The simple version of Instantiate() takes only one argument: the object to copy.

There's a three-argument form that takes the object to copy, then the target position and rotation for that object.

Something like this:

 var fooClone = Instantiate(foo, Vector3.zero, Quaternion.identity);

Now, let's suppose you have two points, v1 and v2. You'd like to spawn an object between them.

 //random point between v1 and v2
 //you already have this, but someone else might read this thread...
 Vector3 pos = Vector3.Lerp(v1, v2, Random.value);

 //vector pointing from v1 to v2
 Vector3 separation = v2 - v1;

 //rotation facing from v1 to v2
 Quaternion rot = Quaterion.LookRotation(separation);

 //spawn
 GameObject copy = (GameObject)Instantiate(someOtherGameObject, pos, rot);

Handy reading:

  • http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Random.html

  • Google "vector subtraction" if the separation formula seems unusual

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 robertbu · Nov 13, 2013 at 01:27 AM

For the object you are putting in the middle, you have to pick a side you are going to have face along the line. So given you are saying the rotation along the 'z', I'm going to assume you want the right side of the object to face the second square. Given the Vector3 positions of the two squares as 'pos1' and 'pos2', you can align the right side as:

 var v3 = pos2 - pos1;
 transform.rotation = Quaternion.FromToRotation(transform.right, v3) * transform.rotation;

Another solution is to fist construct your object you are placing so that the side facing positive 'z' is the one you want to point at block 2 (pos2). Then you can do this:

 Transform.LookAt(pos2, Vector3.forward);
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

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

How to instantiate on custom rotation? 1 Answer

Instantiate not working 2 Answers

Rotating a sprite toward the mouse position on instantiation. 0 Answers

Rotating a certain axis offsets the other ones? 1 Answer

Particle System Instantiate's With Original Rotation - C# 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