Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 21, 2015 at 02:38 PM · rotationinstantiatequaternionrotateaddforce

More problems with arrow shooting

Hello, i have a sprite that rotates and that sprite holds another empty sprite that instantiates arrows. So when i click and hold mouse button and move it the upper body sprite rotates, and on mouse release the arrow instantiates and should add force with AddForce function. Now here are the problems: 1. It instantiates the arrow but not on the the empty sprites and the rotation misses 10 degrees. 2. After instantiating it should AddForce but i need to click and release and than it will add force but than another arrow is created that is standing still. So the when arrow is created it stands still and i need to click twice in order for AddForce to active. 3. And i someone can show me how to get a arrow like flying path because once i add gravity it goes straight but falls pointing in the same direction. Well you know how arrows fly. It forms a rainbow like path. Thats what im trying to get. Here is my code for Upper body

 using UnityEngine;
 using System.Collections;
 
 
 
 public class PlayerController : MonoBehaviour {
 
             
         //Public Vars
         [SerializeField]
         public Rigidbody2D arrowpoint;
         [SerializeField]
         public Camera camera;
         [SerializeField]
         public Rigidbody2D arrow;
         public bool shoot;
         public Vector3 mousePosition;
         public PlayerController play;
         public bool inst;
         //Private Vars
         
         private Vector3 direction;
         private float distanceFromObject;
         private Rigidbody2D rigidbody;
         private bool isAiming;
         private Animator anim;
 
         public static Vector3 dir;
 
 
 
     void Start() {
         rigidbody = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
         arrowpoint = GetComponent<Rigidbody2D> ();
         shoot = false;
         inst = true;
     }
         
     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, arrowpoint.position, transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward));
                 
                 ArrowControl.arrowrb.AddForce(dir.normalized * ArrowControl.thrust);
                 
                 shoot = false;
                 inst = true;
             }
         }
     } 
 }
     

and here is the arrow code

 using UnityEngine;
 using System.Collections;
 
 
 [RequireComponent(typeof(PlayerController))]
 public class ArrowControl : MonoBehaviour {
 
     public static float thrust = 250;
 
     public static Rigidbody2D arrowrb;
 
     // Use this for initializatio
 
     void Start () {
         arrowrb = GetComponent<Rigidbody2D> ();
 
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 }
 

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

0 Replies

· Add your reply
  • Sort: 

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

37 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 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 do i rotate an object 90 or -90 degrees upon collision? 3 Answers

Quaternion.FromToRotation to calculate Rotation between two objects 1 Answer

Any simple ways of keeping track of simple rotation? 2 Answers

Does anyone know how to convert this rotation to lerp 1 Answer

Quaternions cancels transform.Rotate 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