- Home /
Why does the child of a game object not rotate to face mouse position when the parent object is moving?
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;
}
}
Answer by highpockets · May 05, 2020 at 05:43 AM
Is firePoint the child of mainbody?? If so, take the rigidbody off of firePoint. Rigidbodys should never be parented with another rigidbody
You were right that the child should not have a rigid body, I removed that component. I wound up finding a solution to the problem by creating a different object that would constantly move towards the firepoint at a rate faster than the player would ever be able to move. Then I had the object that was following the firepoint be the one that pointed towards the mouse ins$$anonymous$$d of the player. Not sure if there are better ways of doing it but it worked out for me.
public class GunAttatchment : $$anonymous$$onoBehaviour { public Rigidbody2D mainbody; public float speed; public Camera cam; private Transform target; Vector2 mousePos; // Start is called before the first frame update void Start() { //Find the target that you want the object to move towards target = GameObject.FindGameObjectWithTag("FirePoint").GetComponent<Transform>(); } // Update is called once per frame void Update() { //If the distance between the object and the target is greater than 0, have the object move towards the target if(Vector2.Distance(transform.position, target.position) > 0) { transform.position = Vector2.$$anonymous$$oveTowards(transform.position, target.position, speed * Time.deltaTime); } mousePos = cam.ScreenToWorldPoint(Input.mousePosition); } void FixedUpdate() { //Subtracting two vectors results in a vector that points from one to the other 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 = $$anonymous$$athf.Atan2(lookDir.y, lookDir.x) * $$anonymous$$athf.Rad2Deg - 90f; mainbody.rotation = angle; } }