linerenderer start position not always correct
I have a spaceship with 2 guns and I alternate between them. Some times the "laser" lines are drawn perfectly others they come from any point around the gun end. I have been working on this for almost a week and just can't figure out why. Here is the relevant code:
using UnityEngine;
using System.Collections;
public class RaycastShoot : MonoBehaviour {
public int gunDamage = 1;
public float fireRate = 0.12f;
public float hitForce = 100f;
public Transform gunEnd1;
public Transform gunEnd2;
public Camera fpsCam;
private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
private LineRenderer laserLine1;
private LineRenderer laserLine2;
private float nextFire;
private int lastShot = 1;
void Start () {
laserLine1 = GetComponent<LineRenderer>();
laserLine2 = GetComponent<LineRenderer>();
}
void Update () {
if (Input.GetButton ("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, 0.0f));
RaycastHit hit;
if (lastShot == 1) {
laserLine1.SetPosition (0, gunEnd1.position);
} else {
laserLine2.SetPosition (0, gunEnd2.position);
}
StartCoroutine (ShotEffect ());
if (Physics.Raycast (rayOrigin, fpsCam.transform.forward, out hit, 1000)) {
Debug.Log ("Checking for hit");
laserLine1.SetPosition (1, hit.point);
Enemy health = hit.collider.GetComponent<Enemy> ();
if (health != null) {
health.Damage (gunDamage);
}//end if health
} else { //end of raycast
Debug.Log ("nothing to hit");
laserLine1.SetPosition (1, rayOrigin + (fpsCam.transform.forward * 1000));
}//end of raycast else
}//end of fire1
}//end of update
private IEnumerator ShotEffect() {
if (lastShot == 1) {
laserLine1.enabled = true;
lastShot = 2;
} else {
laserLine2.enabled = true;
lastShot = 1;
}
yield return shotDuration;
laserLine1.enabled = false;
laserLine2.enabled = false;
}//end of IEnumerator
}
Most of the code comes from an Unity tutorial. I have removed all the dynamic GameObject (GO) assignments and placed the actual object on the variable in the inspector.
On the ship there is a GO called weapons. Under that there are 2 capsule GOs called Gun (1) and Gun (2) under each gun there is Transforms called gunEnd(#).
The gunEnd transforms are attached to the script like so:
What I don't understand is why the start point is moving while the end point remains correct. The raycast is also working correctly even if the lines are not.
Any help would be greatly appreciated.
Richard M.
have not reviewed the whole things, but I suspect this line will cause you problems:
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, 0.0f));
the z coordinate in the vector parameter defines the distance from the camera. If you use distance 0, you will always get the camera's world position. Try using fpsCam.nearClipPlane
ins$$anonymous$$d of 0.0f
.
Or if it's actually the cemera position that you want: you can just use fpsCam.position
@Glurth As far as I understand it the rayOrigin is just for the raycast to see if there is any object within 1000 unitys of the "middle of the screen"; not the linerenderer.
Or am I missing something?
Richard $$anonymous$$.
For the raycast: I was just saying you could simplify it (since you are using only the center of the screen), and eli$$anonymous$$ate the ViewportToWorldPoint like this:
if(Physics.Raycast (fpsCam.transform.position, fpsCam.transform.forward, out hit, 1000))
But note, this line indicates that rayOrigin DOES in fact affect the line-renderer:
laserLine1.SetPosition (1, rayOrigin + (fpsCam.transform.forward * 1000));
Still though, not sure if this is the actual cause of your problem or not- just waned to throw it out there.
Edit: another thought- make sure the line renderer is specifying WORLD coordinates, not local.
Wanted to throw my opinion in too.
You seem to be using two different intervals, one for firing and one for the shot animation, your beams.
It's just that I think the coroutine get out of sync with your shooting probably toggling lastshot every too often keeping old positions while firing off again.
Just a side note, but it seems it's two lasers behaving the same. It might be more readable putting the laser code into another script and just keep the fire left, fire right, fire left... while they handle themselves how they look. cleaner code you know
On lines 18 and 19, I think you set both laserLines to the same LineRenderer Component. Shouldn't they be different components? $$anonymous$$aybe you should make the laserLines public and assign a different LineRenderer to each.
Your answer
![](https://koobas.hobune.stream/wayback/20220612100430im_/https://answers.unity.com/themes/thub/images/avi.jpg)