- Home /
Unity3d:Projectile(Arrow of Bow) is not rotating as per trajectory.
I am making Archery Game, i have created trajectory and when i throw the arrow of bow ,the trajectory is created as per the stretching using mouse , then arrow is traveling very well as per trajectory but the only problem is that It is not rotating as per the trajectory means it is not traveling & rotating in realistic way.Please tell me which things i need to consider , and also give some reference which will be helpful to me to create archery game from scratch. Thanks in advance.
Answer by robertbu · Mar 06, 2014 at 06:32 AM
Please tell me which things i need to consider , and also give some reference which will be helpful to me to create archery game from scratch.
For discussion/design questions, we ask you take them to Unity Forums. Unity Answers focuses on single, specific technical questions. The layout of Unity Forums is better for discussion questions.
As for your specific question about having the arrow follow the trajectory, if you've constructed your arrow so that the front of the arrow points at positive 'z' when the rotation is (0,0,0), you can do the following each frame to solve your problem:
transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
Hi,
Thank you very much for your response.
I have followed your suggestion, but transform.rotation is not working .
I have done with this functionality after a very long & deeply study about physics and maths and lots of reserach.
$$anonymous$$y code is as follows:-
using UnityEngine;
using System.Collections;
// calculate drag and set the speed
// fire projectile(Arrow) in its +ve X direction.
//Rotate as in gideros.
public class bowScript : $$anonymous$$onoBehaviour {
public GameObject bulletPrefab;
//Clone of the bullet
GameObject Clone;
Vector2 mousePos;
Vector3 screenPos;
bool isDragged;
bool fired;
public float hSliderValue = 0.0f;
void Start()
{
mousePos = new Vector2 ();
screenPos = new Vector3 ();
fired = false;
isDragged = false;
}
void Update (){
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
if(hit.transform.gameObject.name == "bow")
{
if(!isDragged)
{
isDragged = true;
}
}
}
}
if(Input.Get$$anonymous$$ouseButtonUp(0))
{
if(isDragged)
{
isDragged = false;
FireBullet();
}
}
if(isDragged)
{
mousePos = Input.mousePosition;
screenPos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, transform.position.z - Camera.main.transform.position.z));
float temp_Z = $$anonymous$$athf.Atan2((screenPos.y - transform.position.y), (screenPos.x - transform.position.x))*$$anonymous$$athf.Rad2Deg;
if(temp_Z>=-10 && temp_Z<=90)
{
transform.eulerAngles = new Vector3(0, 0, temp_Z);
}
}
if (fired && !Clone.GetComponent<stick$$anonymous$$e>().isSticked) {
Debug.Log("Done");
Vector3 vel = Clone.rigidbody.velocity;
float angle = $$anonymous$$athf.Atan2(vel.y,vel.x)*$$anonymous$$athf.Rad2Deg;
print(angle);
Clone.transform.eulerAngles = new Vector3(0, 0, angle);
}
}
void OnGUI() {
hSliderValue = GUILayout.VerticalSlider(hSliderValue, 10.0f, 20.0f);
GUILayout.Label("Force : "+hSliderValue.ToString());
}
public void FireBullet(){
fired = true;
Clone = Instantiate(bulletPrefab, transform.position + new Vector3(0f,0f,2f), transform.rotation) as GameObject;
Clone.rigidbody.velocity = transform.TransformDirection(Vector3.right * hSliderValue);
}
}
Your code is a bit strange. Your rotation on the 'z' axis, the use of Atan2() and the use of Vector3.right indicate this is a 2D application played on XY plane. Yet you are using Rigidbody rather than Rigidbody2D. The code I gave you was for a typical 3D application. For a typical 2D application with right side of your object considered front, you can do this:
Vector3 dir = rigidbody.velocity;
float angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
This needs to be done every frame. I suggest you put it in a script on the arrow, not put the logic with the firing code as you've done here.
Answer by sarthakshah · Sep 17, 2014 at 11:38 AM
I want to display trajectory in forward direction instead of right, and for that i have changed vector3.forward, but can't get success, so please suggest me for the same.
Thank you.
Hi,
i have done with "Trajectory Simulation in 3D Space or in Forward Direction.
Just check my answer on below link,
Cheers.....
Your answer
Follow this Question
Related Questions
Arrow visual aim helper 0 Answers
transform.position innacurate? [Unity 2D] 1 Answer
Add force to player at a certain z angle in 2D 1 Answer
How to make an arrow fly realistically. 3 Answers
Waves of birds 0 Answers