Question by
Yoctometric · Jan 25, 2018 at 02:07 PM ·
rigidbody2drotate objectchild objectparent transformparent and child
How to make child gameobject follow parent gameobject with rigidbody?
I am having trouble with a simple little 2D game that I wish to make of a person that can wave a shovel about with 360 degrees of motion and whack snow projectiles around.
To accomplish this, I had to make the shovel have collision set to continuous, and the only way I know how to do this is to add a rigidbody2D to it. This leads to other problems, such as the arm of the character falling away from the player and spazzing out on the ground.
How can I make the child have a rigidbody but also be anchored to its parent?
Here is the script for the arm:
using UnityEngine;
using System.Collections;
public class ArmRotator : MonoBehaviour {
public float speed;//speed at which the arm rotates
public Transform parent;//position of player
public Transform child;//position of arm/shovel
void Update () {
Vector2 direction = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position; //calculating the angle part 1
float angle = Mathf.Atan2 (direction.y, direction.x) * Mathf.Rad2Deg; //calculating the angle part 2
Quaternion rotation = Quaternion.AngleAxis (angle, Vector3.forward); // storing the angle
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, speed * Time.deltaTime); //applying the rotation
child.position = parent.position; //my unsuccessful attemt to make the arm follow the parent
}
Comment