Raycasting Issues

Im making a space game where im using raycasts to cast a ray till some 1000 blocks and checking for enemies. If the ray hits an enemy the enemy will lose HP. I can adjust the amount of damage the ray does and i added particle effects too. But there is some problem with the angling of the ray because i was not able to properly give the rotation of the spaceship in the code

I actually modified the script for sprite based shooting to fit for raycast shooting. Plase suggest me ways to fix the code!!

using UnityEngine;

public class PlayerMovementScript : MonoBehaviour
{
    private Rigidbody2D rb;
    public Camera cam;

    Vector2 movement;
    Vector2 mousePos;
    Vector2 direction;
    
    float angle;
    public float moveSpeed;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    }

    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);

        direction = mousePos - rb.position;
        angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

        rb.rotation = angle - 90f;
    }
}

ezgif.com-video-to-gif (2)

PLEASE HELP!!!

There’s actually no raycasting happening in that code. Based on the gif, the rotation looks correct, so the code you shared is fine. Can you share the raycasting code?

Yeah this is just the code where i fix the rotation of my player.

This is the Ray Casting code

using System.Collections;
using Unity.Mathematics;
using UnityEngine;

public class RayCastShootingScript : MonoBehaviour
{
    public Transform bulletOrign;
    public LineRenderer lineOfFire;
    public ParticleSystem explosion;

    public int damage;

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            StartCoroutine(shoot());
        }
    }

    IEnumerator shoot()
    {
        RaycastHit2D hitInfo =  Physics2D.Raycast(bulletOrign.position, bulletOrign.up);

        if (hitInfo)
        {
            EnemyAnimationScript Enemy =  hitInfo.transform.GetComponent<EnemyAnimationScript>();
            
            if (Enemy != null)
            {
                Enemy.takeDamage(damage);
            }

            Instantiate(explosion, hitInfo.transform);

            lineOfFire.SetPosition(0, bulletOrign.position);
            lineOfFire.SetPosition(1, hitInfo.transform.position);
        }
        
        else
        {
            lineOfFire.SetPosition(0, bulletOrign.position);
            lineOfFire.SetPosition(1, bulletOrign.position + bulletOrign.up * 100);
        }

        lineOfFire.enabled = true;

        yield return new WaitForSeconds(0.02f);

        lineOfFire.enabled = false;
    }
}

I noticed that when i rotated the player, the axes don’t change their position relative to rotation. So i thought that might have been the problem

OK, So I put these scripts on some objects and it shoots like it’s meant to. I had to guess the hierarchy and it worked for me, so I suspect your layout is not correct. What does your character layout look like? Where is the RayCastShootingScript? Where is the PlayerMovementScript?

RayCastShootingScript and PlayerMovementScript are both on the player

I don’t really understand. I have moved the scripts and layout to match yours and it still works fine for me
shoot
If you want, you can zip your project and upload it somewhere so I can take a look. It will be a little later 'cos it’s 4am and I need to sleep :sleeping:

yeah lol :joy:

Maybe I will upload on GIT Hub

Maybe I will try again and still if don’t get it, then I will get back to you

I’m a fool. The thing is i had made a sprite based shooting system before this so I made a rectangle around the play area to destroy the bullet sprites. But now while doing RayCasting, if(hitinfo) is considering the collision with that too as a hit so its behaving weirdly.

The same code is working now lol

I spent an unhealthy amount of time in this :slightly_smiling_face:

Oh, yeah. I would not have found that issue without you uploading your project. Good find, though. Have fun

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms