- Home /
Line Renderer
How would i apply a line renderer to follow the ray? also how would i add a distance in which the ray stops?
using UnityEngine;
using System.Collections;
public class PlayerShooting : MonoBehaviour {
float cooldown = 0;
WeaponData weaponData;
public GameObject FireArea ;
void Start() {
}
// Update is called once per frame
void Update () {
cooldown -= Time.deltaTime;
if(Input.GetButton("Fire1")) {
// Player wants to shoot...so. Shoot.
Fire ();
}
}
void Fire() {
if(weaponData==null) {
weaponData = gameObject.GetComponentInChildren<WeaponData>();
if(weaponData==null) {
Debug.LogError("Did not find any WeaponData in our children!");
return;
}
}
if(cooldown > 0) {
return;
}
Debug.Log ("Firing our gun!");
Ray ray = new Ray( FireArea.transform.position, FireArea.transform.forward);
Transform hitTransform;
Vector3 hitPoint;
hitTransform = FindClosestHitObject(ray, out hitPoint);
if(hitTransform != null) {
Debug.Log ("We hit: " + hitTransform.name);
// We could do a special effect at the hit location
// DoRicochetEffectAt( hitPoint );
Health h = hitTransform.GetComponent<Health>();
while(h == null && hitTransform.parent) {
hitTransform = hitTransform.parent;
h = hitTransform.GetComponent<Health>();
}
// Once we reach here, hitTransform may not be the hitTransform we started with!
if(h != null) {
// This next line is the equivalent of calling:
// h.TakeDamage( damage );
// Except more "networky"
PhotonView pv = h.GetComponent<PhotonView>();
if(pv==null) {
Debug.LogError("Freak out!");
}
else {
h.GetComponent<PhotonView>().RPC ("TakeDamage", PhotonTargets.AllBuffered, weaponData.damage);
}
}
}
cooldown = weaponData.fireRate;
}
Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint) {
RaycastHit[] hits = Physics.RaycastAll(ray);
Transform closestHit = null;
float distance = 0;
hitPoint = Vector3.zero;
foreach(RaycastHit hit in hits) {
if(hit.transform != this.transform && ( closestHit==null || hit.distance < distance ) ) {
// We have hit something that is:
// a) not us
// b) the first thing we hit (that is not us)
// c) or, if not b, is at least closer than the previous closest thing
closestHit = hit.transform;
distance = hit.distance;
hitPoint = hit.point;
}
}
// closestHit is now either still null (i.e. we hit nothing) OR it contains the closest thing that is a valid thing to hit
return closestHit;
}
}
ive already looked at the docs and many other threads but i still cant find where i would put it on my script
That would depend on how you want it to behave, I guess. Give it a go, if you don't like it, change it.
well if i dont have a clue where to put it how would i even begin
Answer by Tehnique · Jul 08, 2014 at 12:48 PM
hitTransform = FindClosestHitObject(ray, out hitPoint);
if(hitTransform != null) {
Debug.Log ("We hit: " + hitTransform.name);
LineRenderer MyLineRenderer = FireArea.AddComponent ("LineRenderer") as LineRenderer;
MyLineRenderer.SetColors (new Color(100,0,0,100), new Color(0,0,100,100));
MyLineRenderer.SetWidth(10, 1);
MyLineRenderer.SetVertexCount(2);
MyLineRenderer.SetPosition(0,FireArea.transform.position);
MyLineRenderer.SetPosition(1,hitTransform.position);
...
I'm not able to open Unity right now, so you might have to tweak it a little bit. Also pick your colors, vertex count, etc. Also, add the renderer to yout FireArea only once, at runtime in Start, or from the scene.
ok thats close to what i want now how would i go about adding a liefetime to it so it dosent stay for ever and also how do i make it so i can create more that one without it saying their is already one in the scene
You can use the "enabled" property to turn it off after some time. As for more line renderers, here it says "if you need to draw two or more completely separate lines, you should use multiple GameObjects, each with its own Line Renderer."
You can also .SetVertexCount(0) to deactivate it, and use it again later with new points by setting the vertex count again and the positions of the new points.
Your answer
Follow this Question
Related Questions
Warped lines with line renderer 0 Answers
Using line render to draw a graph 0 Answers
How to draw two lines at the same time that keeps overlapping each other? 1 Answer
Lightning Using A Line Renderer 0 Answers