So I got back into unity after 3 years so I’m pretty much beginning again from scratch.
I’m making a game where you throw a spear at a block and when you hit the block it should output win in the debug.log. however I noticed my collider isn’t even moving along with my object.
In order to make the spear fall realistically I made it out of 3 different parts with different weights and it works like a charm. I wanted to add the boxcollider to the tip of the spear but then found out that the OnCollisionEnter does not work on children for some reason so i put a rigidbody and a collider on the parent. but when I apply a force on the Handle the spear flies away nice and easy but the box collider
that I put on the spear stays behind .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class spearMovement : MonoBehaviour
{
public Rigidbody rb_tail;
public Rigidbody rb_point;
public Rigidbody rb_handle;
public Transform Spear;
public Transform Handle;
public float ThrowAngle = 60f;
public float ThrowForce = 10000f;
public Slider slider;
private bool Thrown = false;
// Start is called before the first frame update
void Start()
{
rb_tail.useGravity = false;
rb_point.useGravity = false;
}
// FixedUpdate is called once per physicsframe
void FixedUpdate()
{
if (Thrown == false)
{
Spear.localEulerAngles = new Vector3(slider.value, 0, 0); //aim the spear by setting the rotation according to a slider
}
/* I tried this to set the transform of the spear the same as the handle
if(Thrown == true)
{
Vector3 HandlePosition = Handle.position;
Quaternion HandleRotation = Handle.rotation;
Spear.position = HandlePosition;
Spear.rotation = HandleRotation;
}
*/
}
void Update()
{
if (Input.GetKeyDown("b"))
{
Throw(slider.value);
}
}
void Throw(float Angle)
{
Debug.Log(Angle);
float rad = Angle * Mathf.Deg2Rad;
rb_handle.AddForce(0, ThrowForce*Mathf.Cos(rad), ThrowForce*Mathf.Sin(rad)); //apply a force to the handle
rb_tail.useGravity = true;
rb_point.useGravity = true;
Thrown = true;
}
}