- Home /
Rotating an Arrow
Before starting, yes I have seen this question http://answers.unity3d.com/questions/14899/realistic-rotation-of-flying-arrow.html but the solutions suggested there are not working for me.
I have a script to shoot. Muzzle is an empty GameObject with the point where the bullet will be fired from. I apply a rotation because the arrow is in vertical when I instatiate it.
using UnityEngine;
public class Shoot : MonoBehaviour
{
public Transform muzzle;
public Rigidbody bullet;
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
Rigidbody b = GameObject.Instantiate(bullet, muzzle.position, muzzle.rotation) as Rigidbody;
b.transform.Rotate(90, 0, 0);
b.AddForce(b.transform.up * 500);
}
}
}
I tried both solutions suggested in the first link to control the arrow rotation, but it doesn't fly the way one should expect. The arrow doesn't keep the initial rotation and reorients itself in a wrong way. The script attached to the arrow is this:
using UnityEngine;
public class ArrowControl : MonoBehaviour
{
public void Start()
{
GameObject.Destroy(gameObject, 10);
}
public void Update()
{
transform.forward = Vector3.Slerp(transform.forward, rigidbody.velocity.normalized, 10 * Time.deltaTime);
//rigidbody.rotation = Quaternion.LookRotation(rigidbody.velocity);
}
}
Any suggestion to make the arrow behave properly during the flight?
Answer by aldonaletto · Oct 11, 2011 at 09:07 PM
I may be wrong, but it seems that you are the culprit! You've instantiated the arrow, rotated it to the correct orientation (transform.up seems to be the arrow tip) and fired it in the correct direction, but while flying you're aligning transform.forward to the velocity direction. You should replace transform.forward by transform.up in both places in the Slerp instruction.
Answer by Ain Soph · Oct 12, 2011 at 03:19 AM
I think you're right, I managed to solve the problem with another solution, using
transform.LookAt(transform.position - rigidbody.velocity);
transform.Rotate(90, 0 , 0);
Thanks for your help
Both Naletto's and Soph's solutions work equally well. The only problem I'm encountering with both is that the arrows start "dancing" upon impact, rather than clattering to rest on the ground.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Add force problem 1 Answer
Rotating child doesn't move parent Rigidbody? 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer