Search code examples
c#unity-game-enginegame-physics

Unity Line Renderer Wont follow raycast?


Cant get the line renderer to follow the raycast? Here is the raycast code as well as the bullet trail code. The line render shoots out as it is supposed to, however it just goes in a straight line in front of the player and not where the player clicks/raycast shoots out.

public class Weapon : MonoBehaviour
{
public float fireRate = 0;
public float Damage = 0;
public LayerMask whatToHit;

public Transform BulletTrailPrefab;

float timeToFire = 0;
Transform firePoint;

// Start is called before the first frame update
void Awake() {
    firePoint = transform.Find("FirePoint");
    if (firePoint == null)
    {
        Debug.LogError("No Firepoint?");
    }
}

// Update is called once per frame
void Update()
{
    if (fireRate == 0)
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
    }
    else
    {
        if (Input.GetButton("Fire1") && Time.time > timeToFire)
        {
            timeToFire = Time.time + 1/fireRate;
            Shoot();
        }
    }
}
    void Shoot()        { 

        Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
        Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
        RaycastHit2D hit = Physics2D.Raycast(firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
        Effect();
        Debug.DrawLine(firePointPosition, (mousePosition - firePointPosition)*100, Color.cyan);
        if (hit.collider != null)
        {
            Debug.DrawLine(firePointPosition, hit.point, Color.red);
        Debug.Log("Hit " + hit.collider.name + "and did" + Damage + "Damage");
        }
    }
    void Effect()
{
    Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation); 
}
}

and the bullet trail:

public class Trail : MonoBehaviour
{
public int moveSpeed = 20;

 // Update is called once per frame
void Update()
{
    transform.Translate (Vector3.right * Time.deltaTime * moveSpeed);
    Destroy(GameObject, 1);
}
}

Any help would be greatly appreciated


Solution

  • My guess: Your issue is that you are not rotating firePoint to point towards the mouse position.

    Therefore using

    Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation); 
    

    will probably equal

    Instantiate(BulletTrailPrefab, firePoint.position, Quaternion.identity); 
    

    and therefore

    transform.Translate (Vector3.right * Time.deltaTime * moveSpeed);
    

    always moves only in world X direction.


    I would use a dedicated method to initialize the spawned object with requried information and also move the Destroy call there so it is done only once:

    public class Trail : MonoBehaviour
    {
        public int moveSpeed = 20;
    
        private Vector2 _direction;
        private bool _isInitialized;
    
        public void Initialize(Vector2 direction)
        {
            _direction = direction;
            _isInitialized = true;
    
            Destroy(gameObject, 1);
        }
    
        // Update is called once per frame
        void Update()
        {
            if(!_isInitialized) return;
    
            transform.Translate (_direction * Time.deltaTime * moveSpeed);
        }
    }
    

    and then use it like

    // Use the correct type here
    public Trail BulletTrailPrefab;
    
    private void Shoot()        
    { 
        Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 firePointPosition = firePoint.position;
        var direction = (mousePosition-firePointPosition).normalized;
        var hit = Physics2D.Raycast(firePointPosition, direction, 100, whatToHit);
    
        // no pass in the direction
        Effect(direction);
    
        Debug.DrawLine(firePointPosition, direction * 100, Color.cyan);
    
        if (hit.collider != null)
        {
            Debug.DrawLine(firePointPosition, hit.point, Color.red);
            Debug.Log("Hit " + hit.collider.name + "and did" + Damage + "Damage");
        }
    }
    
    private void Effect(Vector2 direction)
    {
        var bullet = Instantiate(BulletTrailPrefab, firePoint.position, firepoint.rotation);
        bullet.Initialize(direction);
    }