Hello!
I have made some Star Trek phaser beams for my ship, the shoting part works as it should do.
The beams starting point is set to the 6 lower beam banks on the ship, when you press spacebar you see the blue phaser from the exact point you have sett them to start from.
However… if you move the ship while you shooting, the beams do not follow the beam points where they are set to, they just stays at the position where they first was beamed and pointing at the enemy target.
Its like you leave the beam lines there while you moving away from them.
I made it work this way
1: i have the “PlayerShooting” script on the space ship with the phaser prefab draged to the script.
2: a phaser prefab in the project (not in the scene) thats is set to selfdestruct, a line renderer and the projectile script.
Please help me “Lock” the beams at the BeamPoints.
PlayerShooting and BeamProjectile script
using UnityEngine;
using System.Collections;
public class BeamProjectile : MonoBehaviour {
public int length = 50;
public float noise = 0.1f;
public Transform target;
private LineRenderer lineRenderer;
void Start () {
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.SetVertexCount (length+1);
}
public void Update () {
RenderLaser(transform.position, target.position);
}
void RenderLaser(Vector3 startPos, Vector3 endPos) {
Vector3 targetVector = endPos - startPos;
float lineLength = targetVector.magnitude;
targetVector = targetVector.normalized * lineLength / length;
Vector3 ortho = Vector3.Cross (targetVector, Vector3.up).normalized;
Vector3 ongoing = startPos;
lineRenderer.SetPosition(0, startPos);
for(int i = 1; i <= length; i++) {
ongoing += targetVector;
Vector3 offset = Quaternion.AngleAxis(Random.Range(0.0f, 360.0f), targetVector) * (ortho * Random.Range (0.0f, noise));
lineRenderer.SetPosition(i, ongoing + offset);
}
}
}
using UnityEngine;
using System.Collections;
public class PlayerShooting : MonoBehaviour {
public float coolDown = 0f;
public float fireRate = 0f;
//Checks to se if We actually firing
public bool isFiring = false;
//firing point transform for launching beams
public Transform lowerLeftBeamPoint;
public Transform lowerLeftRBeamPoint;
public Transform lowerRightBeamPoint;
public Transform lowerRightRBeamPoint;
public Transform lowerCenterBeamPoint;
public Transform lowerCenterRBeamPoint;
public Transform upperLeftBeamPoint;
public Transform upperRightBeamPoint;
public Transform upperCenterBeamPoint;
//Beam object
public GameObject beamPrefab;
public AudioSource beamFXSound;
void Start () {
isFiring = false;
}
// Update is called once per frame
void Update () {
CheckInput ();
coolDown -= Time.deltaTime;
if(isFiring == true)
{
// the player has shooting the beam!
Fire();
}
}
void CheckInput()
{
if(Input.GetKeyDown("space"))
{
isFiring = true;
}
else
{
isFiring = false;
}
}
void Fire()
{
if (coolDown > 0)
{
return; //Dont fire
}
// Play the sound effect when player is firing
if (beamFXSound != null)
{
beamFXSound.Play ();
}
GameObject.Instantiate (beamPrefab, lowerCenterBeamPoint.position, lowerCenterBeamPoint.rotation);
GameObject.Instantiate (beamPrefab, lowerRightBeamPoint.position, lowerRightBeamPoint.rotation);
GameObject.Instantiate (beamPrefab, lowerLeftBeamPoint.position, lowerLeftBeamPoint.rotation);
GameObject.Instantiate (beamPrefab, lowerCenterRBeamPoint.position, lowerCenterBeamPoint.rotation);
GameObject.Instantiate (beamPrefab, lowerRightRBeamPoint.position, lowerRightBeamPoint.rotation);
GameObject.Instantiate (beamPrefab, lowerLeftRBeamPoint.position, lowerLeftBeamPoint.rotation);
coolDown = fireRate;
}
}