- Home /
Not Able to Convert 2D trajectory(right Side) into 3dTrajectory(forward Direction)...
Hi,
I have made one demo of archery type game, in that i have written code for trajectory in 2d space(from many reference) and it is displayed in left-right direction, but same thing, i want to do for forward-backward direction (3d Space), i have tried to set z vlaues of position as well as rotation, but can't get in right way.
The below code is working for 2d trajectory, Please help me how do i make it workable for 3d space also.
Thank You.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BoweasyLevel : MonoBehaviour
{
private List<GameObject> trajectoryPoints;
public GameObject TrajectoryPointPrefeb;
private GameObject ball;
private float power = 25;
private int numOfTrajectoryPoints = 30;
public GameObject arrowPrefab;
//Clone of the bullet
GameObject Clone;
Vector2 mousePos;
Vector3 screenPos;
bool isDragged;
bool fired;
public float hSliderValue = 10.0f;
void Start()
{
mousePos = new Vector2 ();
screenPos = new Vector3 ();
fired = false;
isDragged = false;
hSliderValue = 10f;
trajectoryPoints = new List<GameObject>();
for(int i=0;i<numOfTrajectoryPoints;i++)
{
GameObject dot= (GameObject) Instantiate(TrajectoryPointPrefeb);
dot.renderer.enabled = false;
trajectoryPoints.Insert(i,dot);
}
}
void Update (){
if(Input.GetMouseButtonDown(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.GetMouseButtonUp(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 = Mathf.Atan2((screenPos.y - transform.position.y), (screenPos.x - transform.position.x))*Mathf.Rad2Deg;
//if(temp_Z>=-10 && temp_Z<=90)
//{
transform.eulerAngles = new Vector3(0, 0, temp_Z);
setTrajectoryPoints(transform.position, transform.TransformDirection(Vector3.right * hSliderValue));
}
if (fired && !Clone.GetComponent<stickMe>().isSticked) {
Debug.Log("Done");
Vector3 vel = Clone.rigidbody.velocity;
float angle = Mathf.Atan2(vel.y,vel.x)*Mathf.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());
GUI.Label (new Rect(Screen.width/2-75,50,150,50),Application.loadedLevelName);
}
public void FireBullet(){
fired = true;
Clone = Instantiate(arrowPrefab, transform.position + new Vector3(0f,0f,1f), transform.rotation) as GameObject;
print (Clone.transform.position.z);
Clone.rigidbody.velocity = transform.TransformDirection(Vector3.right * hSliderValue);
}
void setTrajectoryPoints(Vector3 pStartPosition , Vector3 pVelocity )
{
float velocity = Mathf.Sqrt((pVelocity.x * pVelocity.x) + (pVelocity.y * pVelocity.y));
float angle = Mathf.Rad2Deg*(Mathf.Atan2(pVelocity.y , pVelocity.x));
float fTime = 0;
fTime += 0.1f;
for (int i = 0 ; i < numOfTrajectoryPoints ; i++)
{
float dx = velocity * fTime * Mathf.Cos(angle * Mathf.Deg2Rad);
float dy = velocity * fTime * Mathf.Sin(angle * Mathf.Deg2Rad) - (Physics2D.gravity.magnitude * fTime * fTime / 2.0f);
Vector3 pos = new Vector3(pStartPosition.x + dx , pStartPosition.y + dy ,2);
trajectoryPoints[i].transform.position = pos;
trajectoryPoints[i].renderer.enabled = true;
trajectoryPoints[i].transform.eulerAngles = new Vector3(0,0,Mathf.Atan2(pVelocity.y - (Physics.gravity.magnitude)*fTime,pVelocity.x)*Mathf.Rad2Deg);
fTime += 0.1f;
}
}
}
Hi,
Once again after a lot of efforts, i got the solution of my problem.
I have done with :Trajectory Simulation" in 3d Space.
I have modify the above code, in it just replacing x by z at all the places.
Cheers....
Answer by sarthakshah · Sep 30, 2014 at 08:33 AM
Hi,
Once again after a lot of efforts, i got the solution of my problem.
I have done with : "Trajectory Simulation" in 3d Space.
I have modify the above code, in it just replacing x by z at all the places.
Cheers....
Your answer
Follow this Question
Related Questions
how to draw trajectory of rocket for faux gravity in unity???? 0 Answers
Create a 3D trail renderer like Hawk eye simulation 1 Answer
Simulating trajectory 0 Answers
Cloud recognition in Vuforia 0 Answers
app alway quit 1 Answer