So I am new to unity, forgive me if my question is already answered somewhere on the forum.
I am creating as top down shooter, and a problem I am having is that I want my player to be forward facing at all times, but I want the weapon/firepoint to rotate towards wherever the mouse is currently on the screen. This only seems to work when the parent object is not moving, otherwise the childs rotation is locked into place. What would I be doing incorrectly? Any advice would be greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D mainbody;
public Rigidbody2D firePoint;
public Camera cam;
Vector2 movement;
Vector2 mousePos;
void Update()
{
//Input
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
//Movement
//Time.fixedDeltaTime is there because it results in consistent movement speed in game, as to how I am uncertain
mainbody.MovePosition(mainbody.position + movement * moveSpeed * Time.fixedDeltaTime);
Vector2 lookDir = mousePos - mainbody.position;
//For the Z rotation of the object, we need to find the angle that will force the player to turn and change the initial firing position
//will be visualized later in animation if we choose to
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
firePoint.rotation = angle;
}
}