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 EZS · Mar 24, 2014 at 04:30 PM · spacespace shooterspaceshipbeam

My 2d tractor beam is not working

So I have two scripts, one for the ship and one for the object I want to move:

 using UnityEngine;
 using System.Collections;
 
 public class TractorBeamShip : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     internal string ObjName;
     
     int range = 1;
     
     GameObject target;
     
     public Color c1 = Color.cyan;
     
     public Color c2 = new Color(1, 1, 1, 0);
     
     void OnMouseDrag(){
         
         //Find the object named after the "ObjName" variable...
         target = GameObject.Find(ObjName);
         
         //Add a line renderer...
         LineRenderer line = gameObject.AddComponent<LineRenderer>();
         
         //Set it's vertex count to 2...
         line.SetVertexCount(2);
         
         //Don't use world space...
         line.useWorldSpace = false;
         
         //Give it a new Material...
         line.material = new Material(Shader.Find("Particles/Additive"));
         
         //Set the colors...
         line.SetColors(c1, c2);
         
         //Set positions to target's position.
         line.SetPosition(range , target.transform.position);
     }
     void OnMouseUp (){
         //Get this object's line renderer...
         LineRenderer line = gameObject.GetComponent<LineRenderer>();
         
         //And destroy it.
         Destroy(line);
     }
     
     // Update is called once per frame
     void Update() {
         if (Input.GetButtonDown("Jump")){
             print (ObjName);//DUH
         }
     }
 }/*IT STILL DOESN'T WORK!!!!*/

and

 using UnityEngine;
 using System.Collections;
 
 public class TractorBeamScript : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     public TractorBeamShip Ship;
     
     public float range;
     
     public float TractorSpeed;
     
     void OnMouseDrag(/*Empty*/) {
         
         //Get the "ObjName" variable from "Ship" & set it to this object's name...
         Ship.ObjName = name;
         
         //Do some complicated calculations & find "Ship"'s position...
         Vector2 diff = Ship.transform.position - transform.position;
         
         //normalize diff...
         diff.Normalize();
         
         //get the angle to "Ship"...
         float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
         
         //& finaly, Add force towards "Ship"'s position.
         rigidbody2D.AddForce(diff * 1 * TractorSpeed);
     }
     
     // Update is called once per frame
     void Update () {
     //Empty
     }
 }
 /*THIS WORKS FINE*/

The problem is that I cannot seem to be able to make the "beam" part of it, the tractor part works just fine.

Comment
Add comment · Show 2
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 supernat · Mar 24, 2014 at 04:45 PM 0
Share

I don't know what the default value for the line width is, but you aren't calling SetWidth(), so that could be an issue. Did you verify in the editor that the lines are being created and assigned a valid material during runtime? It seems like maybe you should just create one line and delete it when you're finished dragging, rather than create a new line for every drag, but that's more of an optimization (also all the previous lines will continue rendering). Also, the GameObject.Find() method is very slow. You should have an interface method in this script to set the reference game object ins$$anonymous$$d, and call it from whoever deter$$anonymous$$es the target.

avatar image EZS · Mar 24, 2014 at 05:20 PM 0
Share

I also tried sending a message to the Ship object, but I can't figure out how to receive messages. I tried setting the width, and that didn't work.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Mar 24, 2014 at 06:13 PM

As supernate notes, when you create a component in code, it's easy to forget a setting. Easier to add the LineRenderer in the Editor, at least for testing. Then can test it works at all (don't press any buttons -- the stubby thing you made should follow you around.) Then can try just SetPosition(0, Vector3.zero); SetPosition(1, Vector3.forward); in the drag. It should aim due north on a click.

If it's in localSpace, I think the end of the line should be at "target-me", and not target, as you have it.

Then, once it works using a pre-made lineRenderer, you could switch back to code-created, if you you like.

Comment
Add comment · Show 2 · 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 EZS · Mar 26, 2014 at 05:42 PM 0
Share

Once again I'll say: I'm using 2D space.

avatar image Owen-Reynolds · Mar 26, 2014 at 11:27 PM 0
Share

LineRenderers are really made for 3D space, and/or 3+ point lines, with bends. They make a new mesh, and rotate to face the camera. If you're fully 2D, you may as well just use a plane.

But, the math for positioning planes is different. if you want to use a lineRenderer, the math for 3D and 2D is the same (well, Vector2 ins$$anonymous$$d of Vector3.)

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

22 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

unity3d quaternion slerp is off target 0 Answers

Space ship controls locking into place 1 Answer

Prevent 2D spaceship going out the screen 1 Answer

Artificial Gravity "SpaceShip" 1 Answer

G Force Acceleration 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