Hello, I’m trying to implement arrows. Every frame the arrow changes its rotation depending on its velocity, but when it hits something and I try to attach the arrow to the object rotation goes wrong. I’m sure that the problem occurs when changing parent and I tried everything without result. This is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Arrow : MonoBehaviour
{
private Rigidbody rb;
private bool hit;
private Quaternion startingRot;
void Start()
{
rb = GetComponent<Rigidbody>();
hit = false;
startingRot = transform.rotation;
}
void FixedUpdate()
{
if (!hit && rb.velocity.magnitude>0)
{
transform.localRotation = Quaternion.LookRotation(rb.velocity);
}
}
private void OnTriggerEnter(Collider other)
{
hit = true;
rb.constraints = RigidbodyConstraints.FreezeRotation;
transform.SetParent(other.transform, true);
rb.isKinematic = true;
rb.velocity = Vector3.zero;
}
}