- Home /
How do I create a solid GameObject (arrow) made out of multiple child objects (head, shaft, fletching)?
I want to create an arrow made out of the head, shaft and fletching (3 separate objects) each containing a Collider and a Rigidbody component. I need for each part to have different mass and drag.
I managed to make something similar by connecting the head and the fletching to the shaft by 2 2D Distance Joints, however the the result didn't seem like a solid object, all the parts were rotating independently.
Then I tried to attach a script to the parent of those objects with and tried to calculate the angle between the head and the fletching and apply that angle to all of those objects.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ArrowRotation : MonoBehaviour
{
public Transform head;
public Transform fletching;
public Transform shaft;
// Use this for initialization
void Start ()
{
head = this.gameObject.transform.GetChild(0);
fletching = this.gameObject.transform.GetChild(2);
shaft = this.gameObject.transform.GetChild(1);
}
// Update is called once per frame
void Update ()
{
float x1 = head.transform.position.x;
float y1 = head.transform.position.y;
float x2 = fletching.transform.position.x;
float y2 = fletching.transform.position.y;
float deltaY = y2 - y1;
float deltaX = x2 - x1;
float angleInDegrees = Mathf.Atan2(deltaY, deltaX) * 180 / Mathf.PI;
head.transform.eulerAngles = new Vector3(0f,0f,angleInDegrees);
fletching.transform.eulerAngles = new Vector3(0f,0f,angleInDegrees);
shaft.transform.eulerAngles = new Vector3(0f,0f,angleInDegrees);
}
}
It is almost perfect in the air, but the parts still slightly bounce around independently of each other, trying to change the angle, when they touch the ground, while they should be completely stationary. I could just change them to kinematic whenthey hit the ground, but I need them to retain the physics.
Do you have any ideas or is there an easier way to achieve this?
Have you tried setting the 'Freeze' checkboxes on the rigidbody components?
Thanks, it's slightly better, still not perfect though.