Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sarthakshah · Sep 17, 2014 at 11:27 AM · simulationtrajectory

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;
         }
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sarthakshah · Sep 30, 2014 at 08:34 AM 0
Share

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....

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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....

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges