Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by krse22 · Nov 18, 2015 at 03:12 PM · rigidbodyrotategravityaxisforce

Unity 5 arrow shooting

I have a sprite that rotates based on holding a mouse button. So once the left mouse button is released he shoots an arrow. The rotation of the arrow is good. ( maybe it misses couple of degrees but its fine ) . But here is the problem i have an AddForce function and that Add Force function needs to shoot based on rotation of the Arrow. Keep in mind that i want my arrow to have a real curved flying. Here is the code.

 using UnityEngine;
 using System.Collections;
 
 
 [RequireComponent(typeof(PlayerController))]
 public class ArrowControl : MonoBehaviour {
 
     public float thrust = 250f;
 
     private Rigidbody2D arrow;
 
     // Use this for initializatio
     private bool flight;
 
 
     void Start () {
         arrow = GetComponent<Rigidbody2D> ();
         flight = true;
     }
     
     // Update is called once per frame
     void Update () {
 
         ArrowMotion ();
     }
 
     void ArrowMotion(){
 
         if (Input.GetMouseButtonUp(0)){
             flight = true;
             if(flight){
             
                 arrow.AddForce(Vector2.right * thrust);
                 flight = false;
             }
 
         }
     }
 
         
 
     }

And the second script

 using UnityEngine;
 using System.Collections;
 
 
 
 public class PlayerController : MonoBehaviour {
 
             
         //Public Vars
         [SerializeField]
         public Camera camera;
         [SerializeField]
         public Rigidbody2D arrow;
         public bool shoot;
         public Vector3 mousePosition;
         public PlayerController play;
         //Private Vars
         
         private Vector3 direction;
         private float distanceFromObject;
         private Rigidbody2D rigidbody;
         private bool isAiming;
         private Animator anim;
 
 
 
 
 
     void Start() {
         rigidbody = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
         shoot = false;
     }
         
     void FixedUpdate() {
 
         HandleRotation ();
        
 
     }
 
     void Update(){
         HandleShoot();
     }
     
 
     void HandleRotation(){
         
         if (Input.GetMouseButton(0)){
 
             isAiming = true;
             //Grab the current mouse position on the screen
             /*mousePosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Input.mousePosition.z + camera.transform.position.z));
             
             //Rotates toward the mouse
             rigidbody.transform.eulerAngles = - new Vector3(0,0,Mathf.Atan2((mousePosition.y - transform.position.y), (mousePosition.x - transform.position.x))*Mathf.Rad2Deg + 180); */
             Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
             Vector3 dir = Input.mousePosition - pos;
             float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
             
         }
         
     }
 
     void HandleShoot()
     {
         if (isAiming) {
             anim.SetBool("Aiming", true);
         }
         if (Input.GetMouseButtonUp (0)) {
             isAiming = false;
             anim.SetBool ("Aiming", false);
             shoot = true;
         
             if (shoot) {
                 Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
                 Vector3 dir = Input.mousePosition - pos;
                 float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
 
                 Instantiate (arrow, rigidbody.position, transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward));
                 shoot = false;
             }
         }
     } 
 }
     
 

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
0

Answer by _Game_Dev_Dude_ · Nov 18, 2015 at 07:55 PM

You need to figure out what the vector that represents the arrow direction is pointed and use that as your force. Right now you are just using Vector3.right. You already solve for this inside your handle shoot function by finding the "dir" of the arrow.

So in your add force just make the force that you put on the arrow this-

  dir.normalized * SomeForce

The higher SomeForce is, the stronger the force.

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
avatar image
0

Answer by krse22 · Nov 19, 2015 at 11:43 AM

Yes it works i just have a bug where i have to release mouse button twice in order for 'AddForce' to work.. but thanks a lot!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Space,Gravity,RigidBody,Physics Forces 0 Answers

Rigidbody - How to make it jump and fall down fast ? Help needed. 1 Answer

Cap rigidbody movement speed based on input. 1 Answer

Can only go horizontally while walking on walls, and not vertically 0 Answers

Move rigid body to mouse position 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