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 GrumpyLlama · Apr 24, 2013 at 09:39 PM · rotationinstantiatescaleplatformsvectors

Instantiating Platforms between two Vectors

Hi,

For a while now Ive been working on a 2D game which requires the player, to create multiple miniature platforms when they drag the mouse while holding it down (to explain this easier here is a youtube video I found after the initial idea doing the same thing except in 3D (just look at the platform creation : http://www.youtube.com/watch?v=aiOzPCplYY0)). I have already searched for previously asked solutions for this but none seem to really appear so hopefully someone can help me with this!

The main question I want answered is how I would go about writing a script to: 1. when the player first clicks down set the startPosition to the mouse coordinates 2. Start the timer 3. When the timer reaches 1 second (this is just for testing)set the second coordinate to the endPosition 4. calculate the the distance between the 2 points 5. calculate the necessary scale up size (not currently working) 6. Instantiate in world coordinates the platform prefab (just a basic plane rotated to 270 degrees on the x axis to be rendered in 2D) 7. set the scale to the discovered distance 8. set the starting point to the current end point to then create another platform 9. restart the timer to repeat :)

My current relevant code:

 var plain : GameObject;
 private var cameraDistance : float = 84.5;
 private var platformStart : Vector2;
 private var platformEnd : Vector2;
 private var timer : float = 0.0;
 private var startTimer : boolean = false;
 
 
 function Start () {
 }
 
 function Update () {
 
 if (Input.GetButtonDown("Fire1") {
 platformStart = Input.mousePosition;
 startTimer = true; 
 }
 
 if (Input.GetButtonUp("Fire1")){
 startTimer = false;
 timer = 0;
 }
 
 if (timer >= 1) {
 platformEnd = Input.mousePosition;
 platformGenerate(platformStart, platformEnd);
 timer = 0.0;
 startTimer = false;
 }
 
 if (startTimer) {
 timer += Time.deltaTime;
 Debug.Log(timer);
 }
 }
 
 function platformGenerate (start : Vector2, end : Vector2) {
 var offsetX = end.x - start.x;
 var offsetY = end.y - start.y;
 var distance = offsetX + offsetY;
 var scale = distance / 2;
 var position : Vector3 = Vector3(start.x + (offsetX / 2), start.y + (offsetY / 2), cameraDistance);
 var placement = Camera.main.ScreenToWorldPoint(position); 
 
 Instantiate(plain, placement, plain.transform.rotation);
 plain.transform.localScale.x = scale;
 startTimer = true;
 platformStart = platformEnd;
 }



Please note all of this code does function but the main issues i'm having are:

  1. I cannot seem to calculate the angle between the 2 points to then set the z axis rotation of the "plain" to. Please also note that because I am creating multiple prefabs I need a script inside each one to set the scale and rotation to rather than the actual prefab itself.

  2. I need to set the local scale of the prefab plane to equal the distance between the 2 points to fill the space, what you I need to do to achieve this as right now the current code just creates ultra long versions of the current plane prefab...

Any help would be hugely appreciated! Thanks GrumpyLlama

Comment
Add comment · Show 5
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 ExTheSea · May 01, 2013 at 05:54 PM 0
Share

If i'm honest it's not surprising to me that your question didn't get an answer yet.

  • Your code isn't formatted.

  • You never really asked a question. You just explained what you already did.

  • This question looks like a "make a script for me"-question.

You should: - Read this page : http://answers.unity3d.com/page/newuser.html - Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

avatar image ExTheSea · May 02, 2013 at 07:24 PM 0
Share

Please reformat your question. First use the code button to make your code be displayed as one. Then you remove the parts which are in there 2 times and also the parts which aren't totally necessary to the question.

Then you may get help. At least i will come back if you do so and will see what i can do. Just don't post another question about the same topic. Rather edit this one and put the text from the new one (which i didn't approved but also am not able to reject because unity answers is being weird lately with the moderation).

avatar image GrumpyLlama · May 04, 2013 at 03:15 PM 0
Share

Apologies for the scrappy post @ExTheSea I have edited the code and question so that hopefully it is more obvious what I am trying to achieve, I would love any more suggestions.

avatar image ExTheSea · May 04, 2013 at 03:22 PM 0
Share

Didn't robertbu's post help you? I had a look at it and it seemed like it's the perfect solution for your problem.

avatar image GrumpyLlama · May 04, 2013 at 03:27 PM 0
Share

@ExTheSea please see the comment I just posted under his script Ive found some issues with it :/

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · May 02, 2013 at 07:19 PM

Here is a bit of code that will position, rotate and scale an object between two vectors. I tested it with a block. Objects that have other orientations (like a cylinder) will require something other than 'Vector3.right' in the rotation calculation. Scale assumes that if the scale is set to (1,1,1), the size of the object will be one unity on a side. The default plane is 10 units on a side. You can use the CreatePlane script to create a plane 1 unit length/width.

 #pragma strict
 
 var v3A = Vector3(-1,1,0);
 var v3B = Vector3(1,-1,0); 
 
 function Start () {
     PositionSize(v3A, v3B);
 }
 
 function PositionSize(v3Start : Vector3, v3End : Vector3) {
                 
     var v3Pos = (v3End-v3Start)/2.0f + v3Start;  // Position
     transform.position = v3Pos;
     
     var v3 = v3End-v3Start;    // Rotation
     transform.rotation = Quaternion.FromToRotation(Vector3.right, v3);
     
     transform.localScale = new Vector3(v3.magnitude, 0.1, v3.magnitude);  // Scale 
 }


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 GrumpyLlama · May 04, 2013 at 03:26 PM 0
Share

@robertbu Thanks for the code but unfortunately I have tried what you suggested and have found 2 major issues:

  1. To make my gameobjects 2D I have removed the z axis and set the rotation of everything rendered to 270 degrees on the x axis if there is a better way to make things 2D then please say so but with your code, "transform.rotation = Quaternion.FromToRotation(Vector3.right, angle);" This sets the right angle required but I also need to set the x axis to equal 270 degrees so that it doesnt face down which I am unsure how to do.

  2. Currently in order that the platforms collide with the other objects when I instantiate them I set the z axis distance to 84.5 units away from the camera which is the required distance however I cannot achieve this if I just simply do "v3End-v3Start"

Therefore currently although this has been helpful, especially with the Quaternion.FromToRotation function this has not helped me reach my intended script.

avatar image robertbu · May 04, 2013 at 04:55 PM 0
Share

I'm away from my computer. I'll take a second look Sunday evening. The CreatePlane script above will allow you to create a plane with a vertical orientation. You might want to consider using a thin cube with a box collider rather than a plane for your platforms. It might do a better job of collision detection.

avatar image robertbu · May 06, 2013 at 02:29 PM 0
Share

I tested the above code with a vertical 1 x 1 plane using the CreatePlane script. It works fine. In order to set the 'Z' position to 84.5, you have to set the z position of both v3Start and v3End to 84.5. Given that the plane mesh is facing out and your objects are co$$anonymous$$g down, you might have to use a box collider on your plane mesh to get the collisions to work right. Note the 'v3End - v3Start' calculates a vector between the two relative to v3Start and is not the position. The actual position calculation is half way between v3Start and v3End, so if both Z coordinates are the same, then the Z position will match both v3Start and v3End.

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

12 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

Related Questions

Having trouble instantiating a gameobject with rotation 0 Answers

Scale issue returns when object is instantiated 1 Answer

Instantiating prefab of object like it was in the scene 0 Answers

Scaled sprite distorts on rotation in different resolutions 0 Answers

Instantiated object not rotating around the center 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