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 James 13 · Feb 22, 2011 at 04:57 PM · rotatescalecube

How to scale and move a cuboid so that it fit's between two points?

Hi, I'm trying to draw a cuboid of static height between where the user clicks the first time, and where the user clicks the second time. I could probably solve this problem using a bit of maths, but I'm hoping there's some clever way of doing it!

Cheers.

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
2

Answer by Bunny83 · Feb 22, 2011 at 05:22 PM

Well, the easiest way of doing that would be to parent your 1x1x1 standard cube in a empty GO but locally translated to Vector3(0, 0, 0.5). That way the center of the parent object is centered on one side of the cube. you just have to rotate the parents forward axis (aka z-axis) towards your target and set the parents z-scale to the distance between A and B.

// part of a C# script that should be on the parent object
void SetTarget(Vector3 target)
{
    Vector3 direction = target - transform.position;
    transform.rotation = Quaternion.LookRotation(direction);
    transform.localScale = new Vector3(1,1,direction.magnitude);
}

(ps I wrote that from scratch without error-checking)

Anyway, I'm not a friend of scaling. I would try a procedural mesh approach. But that's up to you ;)

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 philjhale · May 16, 2013 at 08:45 PM 0
Share

This code didn't quite work for me but it gave me a big push in the right direction. Thanks.

avatar image
0

Answer by Jeston · Feb 22, 2011 at 05:42 PM

Here would be my attempt at trying this: Create a 1x1x1 unit cuboid object, either hidden or in a prefab. When the second click occurs, take the difference in position, transform the distance to the proper coordinate frame and then set the scale based on that distance. Since you have a unity cube made, the distance and scale should be proportional.

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 jpatinop80 · Oct 05, 2012 at 04:24 PM

I don't know if I understand what you said, but this code created a object "cube" setting two points for the scale, just set a cube object as a prefab.

 #pragma strict
 
 public var myCube : Transform;
 public var Ancho : float = 3;
 public var Alto : float = 10;
 
 public var inputPosA : Vector3 = Vector3.zero;
 public var inputPosB : Vector3 = Vector3.zero;
 
 var result : Vector3;
 
 //var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
 function Update() 
 {
 
 
 if (Input.GetMouseButtonDown(0)){
 
      inputPosA = Input.mousePosition;
 
  }
 
  if (Input.GetMouseButtonUp(0)){
 
      inputPosB = Input.mousePosition;
 
        DrawALine();
      }
 
 
    
 }
 
 function DrawALine() 
 {
     Vector3 posC = ((inputPosB - inputPosA) * 0.5F ) + inputPosA; 
 float lengthC = (inputPosB - inputPosA).magnitude; //C#
     var sineC : float = ( inputPosB.y - inputPosA.y ) / lengthC; 
     var angleC : float = Mathf.Asin( sineC ) * Mathf.Rad2Deg; 
     if (inputPosB.x < inputPosA.x) {angleC = 0 - angleC;} 
 
     Debug.Log( "inputPosA" + inputPosA + " : inputPosB" + inputPosB + " : posC" + posC + " : lengthC " + lengthC + " : sineC " + sineC + " : angleC " + angleC );
 
     var myLine : Transform = Instantiate( myCube, posC, Quaternion.identity ); 
     myLine.localScale = Vector3(lengthC, Ancho, Alto); 
 
     myLine.rotation = Quaternion.Euler(0, 0, angleC);
 }
 
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

2 People are following this question.

avatar image avatar image

Related Questions

Rotate a Cube by 90 degree 0 Answers

Cube Sorter 0 Answers

Instantiating Cube Prefabs with scale 3 Answers

3ds rotation problem 1 Answer

¿Is it possible to scale independently of the rotation? 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