I want my arrow to freeze and get stuck to an object upon collision but for some reason my arrow hits an object but then just falls to the ground.
here’s my code im using.
using UnityEngine;
using System.Collections;
public class bowshooting : MonoBehaviour {
public object collision;
GameObject prefab;
void Start () {
prefab = Resources.Load(“arrow”) as GameObject;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
GameObject arrow = Instantiate(prefab) as GameObject;
arrow.transform.position = transform.position + Camera.main.transform.forward;
Rigidbody rb = arrow.GetComponent<Rigidbody>();
rb.velocity = Camera.main.transform.forward * 20;
arrow.transform.rotation = this.transform.rotation;
}
}
// on collision freeze in place and attach to object
void OnCollisionEnter(Collision collision)
{
this.transform.position = collision.contacts[0].point;
this.GetComponent<Rigidbody>().useGravity = false;
collision.rigidbody.isKinematic = true;
collision.rigidbody.velocity = Vector3.zero;
collision.transform.parent = collision.transform;
collision.transform.localPosition = collision.transform.InverseTransformPoint(collision.contacts[0].point);
collision.rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
}
}
,