Optimizing Arrow rotation

Hey there,

It may be a simple question, but this is the situation:

Currently if I shoot an arrow, the arrow will point the direction it is flying (so if it falls down it will face down etc), when it hits a collider, I freeze all its positions and rotations and turn off the gravity. The point is that it still checks it direction it is moving, while it isn’t.

I would like to only face its movement direction while its not grounded (or moving). How do I do that?

This is the code I’ve attached to my arrow object:

using UnityEngine;
using System.Collections;

public class ArrowControl : MonoBehaviour {
	
	public bool isPickup = false;
	public int gravityRotate = 1;
	
	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
		transform.forward = Vector3.Slerp(transform.forward, rigidbody.velocity.normalized, gravityRotate * Time.deltaTime);
	}
	
	void OnCollisionEnter(Collision hit)
	{
		if(hit.gameObject.name != "First Person Controller" || hit.gameObject.tag != "Arrow") {
		rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
		rigidbody.useGravity = false;
		isPickup = true;
		}
	}
}

Thanks!

using UnityEngine;

using System.Collections;

 

public class ArrowControl : MonoBehaviour {

    

    public bool isPickup = false;

    public int gravityRotate = 1;
    private bool bAlreadyHit = false;
    

    // Use this for initialization

    void Start () {

    }

    

    // Update is called once per frame

    void Update () 
{
if(!bAlreadyHit)
{

        transform.forward = Vector3.Slerp(transform.forward, rigidbody.velocity.normalized, gravityRotate * Time.deltaTime);
}

    }

    

    void OnCollisionEnter(Collision hit)

    {

        if(hit.gameObject.name != "First Person Controller" || hit.gameObject.tag != "Arrow") {

        rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;

        rigidbody.useGravity = false;

        isPickup = true;
bAlreadyHit = true;

        }

    }

}

Like that?

When I did arrows like this I did it similar to bullets and bullet holes… Once the arrow hits it is destroyed/recycled and creates a stuck arrow prefab with no scripts attached at the original arrow’s position / rotation. Can also parent the stuck arrow to what it hits that way if the hit object moves… The arrow goes with it.

I was thinking of something simular, but not sure if an if statement is less heavy on CPU than the actual Slerp, does somebody know the answer on that?

That’s actually a nifty trick I haven’t thought of, I think I’ll give it a try.

Thanks for the good responses :smile: