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.