- Home /
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.
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.
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.
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.
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
Follow this Question
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