- Home /
Child object won't follow the player.
I am making a top down isometric game and I used a used a tutorial for top down shooting. It's this one: https://www.youtube.com/watch?v=LNLVOjbrQj4 In it he makes the player face the mouse but for my game I didn't want my player to face the mouse which resulted in it not firing at the mouse. So I applied the code to the empty game object "FirePoint" which is a child of the player but when a child has a rigidbody2D it doesn't follow the player but the aiming works. So if anyone knows how to get it to follow the player or a better method for shooting with mouse aim without rotating the player please leave a comment.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAtMouse : MonoBehaviour
{
// Fix this. FirePoint won't move with the player even though it's a child object of the player
public Rigidbody2D rb;
public Camera cam;
Vector2 mousePos;
//public Transform Parent;//Remember to assign the parent transform
//private Vector3 pos, fw, up;
// Start is called before the first frame update
void Start()
{
//pos = Parent.transform.InverseTransformPoint(transform.position);
//fw = Parent.transform.InverseTransformDirection(transform.forward);
//up = Parent.transform.InverseTransformDirection(transform.up);
}
// Update is called once per frame
void Update()
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
//var newpos = Parent.transform.TransformPoint(pos);
//var newfw = Parent.transform.TransformDirection(fw);
//var newup = Parent.transform.TransformDirection(up);
}
private void FixedUpdate()
{
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
}
check if its static (in the inspector window), it should be off.
Your answer
Follow this Question
Related Questions
Problem with a RigidBody and IsKinematic... I think 1 Answer
My flipped character ragdoll has wrong hingejoint anchor positions 0 Answers
Rigidbody2D.IsKinematic giving object reference not set? 0 Answers
Drag to add power, release to throw at opposite angle (RigidBody2D), strange beahviour. 3 Answers
2DColliders going through eachother 1 Answer