Hello Unity Community,
I’m working on a project and basically here are more vital details. I have a gameObject that’s a cube, it’s been a little bit resized. It’s named “first_Portal”, it has a child object which is an empty object called “first_Portal_Laser_Exit”. I have another gameObject called “second_Portal” and it too has an empty child object called “second_Portal_Laser_Exit”.
Both Laser Exits are basically coded to display a laser that seems like it’s coming out of the portals when the players laser makes contact with either first_Portal or second_Portal gameObjects.Here’s some code for a better view.
private LineRenderer laser, first_Portal_Line_Renderer, second_Portal_Line_Renderer;
private GameObject laser_Aim_Point, first_Portal_Exit, second_Portal_Exit;
public Material laser_Material;
private float laser_Range = 15.0f, default_Portal_Range = 100.0f, way_Point_Speed = 35.0f;
private Color color_One = Color.red, color_Two = Color.blue;
private Ray my_Ray, first_Portal_Ray, second_Portal_Ray;
RaycastHit hit;
Vector3 start_Point, end_Point, first_Portal_Start_Point, first_Portal_End_Point, second_Portal_Start_Point, second_Portal_End_Point;
bool activate_First_Portal_Line_Renderer = false, activate_Second_Portal_Line_Renderer = false, FIRED_WAY_POINT = false;
public Rigidbody way_Point;
void Start () {
//Create the laser.
laser = this.gameObject.GetComponent<LineRenderer> ();
laser_Aim_Point = GameObject.Find ("aimPoint");
laser.SetWidth (.02f, .02f);
laser.material = new Material (Shader.Find ("Particles/Additive"));
laser.SetVertexCount (2);
if (laser_Material == null) {
laser.SetColors(color_One, color_One);
}
else{
laser.material = laser_Material;
}
//Set up configuration for the first portal.
first_Portal_Exit = GameObject.Find ("first_Portal_Laser_Exit");
first_Portal_Exit.AddComponent<LineRenderer> ();
first_Portal_Line_Renderer = first_Portal_Exit.GetComponent<LineRenderer> ();
first_Portal_Line_Renderer.SetWidth (.02f, .02f);
first_Portal_Line_Renderer.material = new Material (Shader.Find ("Particles/Additive"));
first_Portal_Line_Renderer.SetVertexCount (2);
first_Portal_Line_Renderer.SetColors (color_Two, color_Two);
//Set up configuration for the second portal.
second_Portal_Exit = GameObject.Find ("second_Portal_Laser_Exit");
second_Portal_Exit.AddComponent<LineRenderer> ();
second_Portal_Line_Renderer = second_Portal_Exit.GetComponent<LineRenderer> ();
second_Portal_Line_Renderer.SetWidth (.02f, .02f);
second_Portal_Line_Renderer.material = new Material (Shader.Find ("Particles/Additive"));
second_Portal_Line_Renderer.SetVertexCount (2);
second_Portal_Line_Renderer.SetColors (color_Two, color_Two);
}
void Update() {
portal_Manager ();
if (GameObject.Find ("way_Point(Clone)") != null) { //WE KNOW THE WAY_POINT EXISTS IN THE SCENE.
Debug.Log ("Fired");
}
}
void LateUpdate() { //LateUpdate renders the laser better.
my_Ray = new Ray (laser_Aim_Point.transform.position, laser_Aim_Point.transform.forward * laser_Range);
start_Point = laser_Aim_Point.transform.position;
end_Point = my_Ray.origin + (my_Ray.direction * laser_Range);
laser.SetPosition (0, start_Point);
if(Input.GetMouseButtonDown(0)) { //CLICK TO FIRE THE WAY_POINT.
if(FIRED_WAY_POINT == false) {
Rigidbody fire_Way_Point = Instantiate(way_Point, laser_Aim_Point.transform.position, laser_Aim_Point.transform.rotation) as Rigidbody;
fire_Way_Point.velocity = transform.TransformDirection(new Vector3(0,0, way_Point_Speed));
GameObject fwp = GameObject.Find ("way_Point(Clone)");
fwp.AddComponent<stick_Effect>();
Destroy (fire_Way_Point.gameObject, 20);
FIRED_WAY_POINT = true;
}
else if(FIRED_WAY_POINT == true){
Debug.Log ("Waypoint already fired.");
}
}
if (Physics.Raycast (my_Ray, out hit, laser_Range)) {
laser.SetPosition(1, hit.point);
if(hit.collider.tag == "first_Portal")
{
new_Distance = laser_Range - hit.distance;
activate_Second_Portal_Line_Renderer = true;
}
else if(hit.collider.tag == "second_Portal")
{
new_Distance = (int)hit.distance;
activate_First_Portal_Line_Renderer = true;
}
}
else{
laser.SetPosition(1, end_Point);
activate_First_Portal_Line_Renderer = false;
activate_Second_Portal_Line_Renderer = false;
}
}
void portal_Manager() {
if (activate_Second_Portal_Line_Renderer == true) {
second_Portal_Ray = new Ray (second_Portal_Exit.transform.position, -second_Portal_Exit.transform.forward * default_Portal_Range);
second_Portal_Start_Point = second_Portal_Exit.transform.position;
second_Portal_End_Point = second_Portal_Ray.origin + (second_Portal_Ray.direction * default_Portal_Range);
second_Portal_Line_Renderer.SetPosition(0, second_Portal_Start_Point);
second_Portal_Line_Renderer.SetPosition(1, second_Portal_End_Point);
}
else{
second_Portal_Line_Renderer.SetPosition (0, Vector3.zero);
second_Portal_Line_Renderer.SetPosition (1, Vector3.zero);
}
if(activate_First_Portal_Line_Renderer == true) {
first_Portal_Ray = new Ray (first_Portal_Exit.transform.position, -first_Portal_Exit.transform.forward * default_Portal_Range);
first_Portal_Start_Point = first_Portal_Exit.transform.position;
first_Portal_End_Point = first_Portal_Ray.origin + (first_Portal_Ray.direction * default_Portal_Range);
first_Portal_Line_Renderer.SetPosition(0, first_Portal_Start_Point);
first_Portal_Line_Renderer.SetPosition(1, first_Portal_End_Point);
}
else{
first_Portal_Line_Renderer.SetPosition(0, Vector3.zero);
first_Portal_Line_Renderer.SetPosition(1, Vector3.zero);
}
}
Now that works, BUT I my main goal is to fire an instantiated gameobject and have that gameObject stick to wherever the hell it landed. After a bit of research I learned about FixedJoints, and tried implementing a way for fixed joints to work with the instantiated rigidbody object. Below is the script called “stick_Effect” I attached to this rigidbody gameObject
void Start() {
}
void Update () {
}
void OnTriggerEnter(Collider other) {
if (other.tag == "first_Portal") {
FixedJoint fj;
fj = this.gameObject.AddComponent<FixedJoint>();
fj.connectedBody = other.rigidbody;
}
}
SOMETIMES It works. As in sometimes I left click and instantiated object lands on “first_Portal” and sticks there. But that only works about 20% of the time, the other 80% it just completely passes through. So am I doing the fixed joint thing wrong? Is there a better way of making the instantiated object stick to wherever it lands?
Also, if it matters, here are a list of the main objects and their game components
first_Portal
Cube (Mesh Filter)
Box Collider
Mesh Renderer
second_Portal
Cube (Mesh Filter)
Box Collider
Mesh Renderer
way_Point (The instantiated gameObject with a rigidbody)
Sphere (Mesh Filter)
Sphere Collider
Mesh Renderer
Light
Rigidbody
Thank you for any help I receive.