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
1
Question by sarthakshah · Mar 06, 2014 at 06:29 AM · trajectoryarrow

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.

Comment
Add comment
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

2 Replies

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

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);
Comment
Add comment · Show 3 · 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
avatar image sarthakshah · Aug 19, 2014 at 06:59 AM 0
Share

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);
         
     }
 
 }

avatar image robertbu · Aug 19, 2014 at 07:17 AM 0
Share

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.

avatar image AndrewRyan · Dec 04, 2014 at 09:00 AM 0
Share

So helpful robert, thanks.

avatar image
0

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.

Comment
Add comment · Show 1 · 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
avatar image sarthakshah · Sep 30, 2014 at 09:01 AM 0
Share

Hi,

i have done with "Trajectory Simulation in 3D Space or in Forward Direction.

Just check my answer on below link,

http://answers.unity3d.com/questions/792481/not-able-to-convert-2d-trajectoryright-side-into-3.html#answer-800275

Cheers.....

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

20 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

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


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