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 skyguy126 · Mar 12, 2015 at 02:42 PM · rotationjavascriptlinerendererboxcollider2d

Spawning a BoxCollider2D between 2 vector3 (JS)

So I am working on a game where a user will draw a line (got that part working) by using input.touch. What I want to have happen is have a BoxColider2D to spawn in the center of the line segment at the correct rotation to match up with the line. I think I can figure out how to spawn it in the center of the line segment but what I don't understand is spawning it at the correct rotation.


Here is the test script I am using to work out core functionality (JavaScript):

 #pragma strict
 var lastDotPosition : Vector3;
 var lastPointExists : boolean = false;
 
 var c1 : Color = Color.yellow;
 var c2 : Color = Color.red;
 var lengthOfLineRenderer : int = 2;
 
     function Start() {
          var lineRenderer : LineRenderer = gameObject.AddComponent.<LineRenderer>();
          lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
          lineRenderer.SetColors(c1, c2);
          lineRenderer.SetWidth(0.01,0.01);
          lineRenderer.SetVertexCount(lengthOfLineRenderer);
     }
 
 function Update()
     {
         if (Input.GetMouseButton(0))
         {
             var lineRenderer : GameObject = new GameObject("Line");
             lineRenderer.AddComponent.<LineRenderer>();
             var newLine : LineRenderer = lineRenderer.GetComponent.<LineRenderer>();
             newLine.SetColors(c1, c2);
             newLine.SetWidth(0.1,0.1);
              newLine.SetVertexCount(lengthOfLineRenderer);
              
              
             var mouse : Vector2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             var mouseRay : Ray2D = new Ray2D(mouse, Vector2.zero);
             var newDotPosition : Vector2 = mouseRay.origin;
             
             lineRenderer.AddComponent.<BoxCollider2D>();
             var newCol : BoxCollider2D = lineRenderer.GetComponent.<BoxCollider2D>();
 
             
             if (newDotPosition != lastDotPosition)
             {
                 var pos : Vector3 = Vector3(lastDotPosition.x,lastDotPosition.y, 0);
                 newLine.SetPosition(0, pos);
                 var pos2 : Vector3 = Vector3(newDotPosition.x, newDotPosition.y, 0);
                 newLine.SetPosition(1, pos2);
                 newCol.size = Vector2(1, .1);
                 var colAngle : float = Vector3.Dot(pos,pos2);
                 colAngle = colAngle/(pos.magnitude*pos2.magnitude);
                 colAngle = Mathf.Acos(colAngle);
                 colAngle = colAngle*180/Mathf.PI;
                 Debug.Log("Col angle: " + colAngle);
                 lineRenderer.transform.Rotate(Vector3(0, 0, colAngle+17.28));
                 Debug.Log("Rotation: " + lineRenderer.transform.rotation.z);
                 lastDotPosition = newDotPosition;
             }
         }
     }


I figured out how to calculate the angle between the two vectors that represent the endpoints of the line segment; pos (point start) and pos2 (point end). What I don't get is getting the gameobject/collider to rotate to that degree. The angle between the two vectors is the var colAngle. Please tell me if I am calculating the angle correctly. If yes then how am I supposed to make the BoxCollider2D rotate to the correct rotation.

To test it just attach this script to an new empty gameobject and run it.

Thanks for your time and ALL answers are appreciated!

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

0 Replies

· Add your reply
  • Sort: 

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

How can I set a limit to my object rotation???(Here's the code) 2 Answers

Change object position on trigger enter 3 Answers

Setting rotation of player equal to rotation of a Line from Line Renderer,Get the rotation of Line Renderer and equal it to the rotation of the player 0 Answers

Store initial rotation in JavaScript 2 Answers

How to make a animation rotation js.script? 2 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