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;
}
}
PLEASE HELP!!!