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 MithosAnnar · May 16, 2012 at 01:23 PM · rotationarrowcs

How do I Fire an Arrow Properly ? [SOLVED]

I am making a 2D game, seen from the left, using textures on planes (some of which have... box colliders). And would like some help to achieve the following:

I have a screen which is 480 pixels wide and 320 pixels high. When I click the screen I make an arrow which has an arrow script attached to it (written in C - Sharp). In this script I record the y position of the click. Depending on how high the click is, determines how far the arrow goes. But the arrow can never go further than 480 pixels, because I don't want it to go off screen.

Any Idea what I could do to get this working?

Thanks to a reasonably easy to understand reply! (and no thanks to an unreasonably hard to understand reply!)

P.S: I am using the XZ - Plane, so if your answer has anything to do with rotation... it should take that into account.

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 MithosAnnar · May 17, 2012 at 11:08 AM 0
Share

Thanks for the help Berenger!

2 Replies

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

Answer by MithosAnnar · May 17, 2012 at 11:07 AM

It took me bloody ages but i finally did it!!

Here it is for anyone who wants to fire an arrow properly.

make a plane with an archer sprite and animation script on him. put an attack script on the archer (just make sure you record the mouse position, so you can send it to your arrow script).

on the arrow script put the following:

ArrowScript.cs

 using UnityEngine;
 using System.Collections;
 
 public class Arrow : MonoBehaviour {
         
 public float damage = 5;
 public float rotationSpeed;
 public float arrowSpeed = 5;
     
 private Transform myTransform;
         
 private Vector3 mousePoint;
     
 private bool canTranslate = true;
 private bool canRotate = false;
     
 void ArrowStart (Vector3 v) {
 
    myTransform = transform;
                                                 
    mousePoint = v;
                         
 if (mousePoint.y > 200) {
             
    myTransform.rotation = Quaternion.Euler(0, 150, 0);
             
    rotationSpeed = Random.Range(2.5f, 5f);
             
    StartCoroutine("CanRotate", Random.Range(0.25f, 0.5f));
             
 }
         
 if (mousePoint.y > 100 && mousePoint.y < 200) {
             
    myTransform.rotation = Quaternion.Euler(0, 170, 0);
                                     
    rotationSpeed = Random.Range(4f, 8f);
             
    StartCoroutine("CanRotate", Random.Range(0.15f, 0.3f));
             
 }
         
 if (mousePoint.y < 100) {
             
    myTransform.rotation = Quaternion.Euler(0, 200, 0);
                                         
    rotationSpeed = Random.Range(20f, 40f);
             
    StartCoroutine("CanRotate", Random.Range(0.05f, 0.1f));
                         
 }
                                     
 }
     
 void Update () {
         
 if (canRotate == true) {
         
    myTransform.Rotate (0, (mousePoint.y / 10) * Time.deltaTime * rotationSpeed, 0);
             
 }
                 
 if (canTranslate == true) {
                                 
    myTransform.Translate(new Vector3(-1, 0, 0) * arrowSpeed * Time.deltaTime);
             
 }
         
 if (myTransform.position.z <= - 2.2f) {
             
    StartCoroutine("Destroy");
             
 }
 
 }
 
 IEnumerator Destroy () {
         
    canTranslate = false;
             
    canRotate = false;
         
    yield return new WaitForSeconds (2);
         
    Destroy(gameObject);
                 
 }
     
 IEnumerator CanRotate (float f) {
         
    yield return new WaitForSeconds (f);
         
    canRotate = true;
         
    yield return new WaitForSeconds (f * 2);    
         
    canRotate = false;
     
 }
     
 }

Special thanks to Berenger for helping me!

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 Berenger · May 16, 2012 at 03:26 PM

You can either find what is the for maximum that can be applied given a certain angle (sounds complicated) or add walls at the borders of the screen. Either you set their position from the scene view, or by script. If the camera is orthogonal, the size of the frustrum orthographicSize 2 for the height, orthographicSize 2 * aspect for the width.

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 MithosAnnar · May 16, 2012 at 04:29 PM 0
Share

Could I have more on the angle theory?

I don't want to do the borders, because I want the arrows to hit the ground, but not go offscreen.

avatar image Berenger · May 16, 2012 at 04:34 PM 0
Share

I don't know if there is a way to calculate an object trajectory given it's mass and the initial force. I'm afraid you'll need to do it empirically. Like, if the angle is > 80, maxForce is 500, > 60 maxForce is 200, > 40 100 etc. It's dirty but it's the best I can think of without the trajectory thing.

avatar image MithosAnnar · May 17, 2012 at 08:23 AM 0
Share

Ok ill try that, thanks for the reply.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Add force to player at a certain z angle in 2D 1 Answer

Turret Rotation Controlled by Arrow Keys 2 Answers

Rotating an Arrow 2 Answers

2D Sprite rotation but still face the camera? 0 Answers

Want to rotate camera horizontally around object using left and right arrow keys 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