Question by
BigRedRedneck · May 31, 2018 at 05:29 AM ·
raycasttransformvector3line rendererinstatiate
How do I specify the origin and end points of the arc1 instantiated line?
The bolt material attached to arc1 spawns at the hand and goes the correct direction most of the time but sometimes it shoots randomly off into space and doesn't instantiate at the hand directly either. This is a script for a basic lightning bolt spell. Any other approaches for a lightning bolt would also be welcome as I am having issues with the line renderer.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LightningShooting : MonoBehaviour {
//Debug.Log("1");
public float range = 100f;
public float timeBetweenBullets = 0.15f;
public float effectsDisplayTime = 0.2f;
public int lengthOfLineRenderer = 100;
float timer;
Ray shootRay;
RaycastHit shootHit;
int shootableMask;
//[SerializeField]
//public Animator arcAnim2;
[SerializeField]
private ParticleSystem sparksCast;
[SerializeField]
private LineRenderer arc1;
[SerializeField]
private AudioSource thunderAudio1;
[SerializeField]
private Light flashCast;
[SerializeField]
public int damagePerShot = 50;
void Awake ()
{
shootableMask = LayerMask.GetMask("Shootable"); //layer of "Shootable" objects
arc1.positionCount = 2;
}
void Update()
{
timer += Time.deltaTime; // sets up the timer
if (Input.GetKeyDown(KeyCode.Alpha2) && timer >= timeBetweenBullets) //if the fire key is pressed and the is timer is also less than timebetweenbullets
{
Shoot(); //defined below
}
}
void Shoot () //what happens when shoot is called
{
timer = 0f; //reset timer
Debug.Log("shoot");
Instantiate<ParticleSystem> (sparksCast, gameObject.transform.position, Quaternion.identity); //emit fromt he particle system
Instantiate<Light>(flashCast, gameObject.transform.position, Quaternion.identity); //light on player on
Instantiate<AudioSource>(thunderAudio1, gameObject.transform.position, Quaternion.identity); // plays audio
Instantiate<LineRenderer>(arc1, gameObject.transform.position, Quaternion.identity); // line for animation placheolder
//Instantiate<Animator>(arcAnim2, gameObject.transform.position, Quaternion.identity);
Vector3[] points = new Vector3[lengthOfLineRenderer];
shootRay.origin = transform.position; //moves ray for ray cast
shootRay.direction = transform.forward; // sets forward to positive z
if (Physics.Raycast (shootRay, out shootHit, range, shootableMask)) //if you hit something in the layer "Shootable"
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent<EnemyHealth>(); //look for script enemyhealth
if(enemyHealth != null) //if the object isn't a player
{
enemyHealth.TakeDamage(damagePerShot, shootHit.point); //take damage, create a point to plac eblood splatter etc.
}
arc1.SetPosition(1, shootHit.point); //draws line for line renderer
}
else //if not on the shootable layer...
{
arc1.SetPosition(1, shootRay.origin + shootRay.direction * range); // set the end point to extent of range
}
}
}
Comment