• 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 Neroner23 · Aug 15, 2012 at 05:13 AM · translaterpgfiring

Firing arrow in direction depending on Camera

Hi there. I wonder how to make this script work.

if(Input.GetMouseButton(1)) { //arrow.useGravity = false;

   vec = new Vector3(Input.mousePosition.x,Input.mousePosition.y,Camera.MainCamera.transform.position.z+1);
     var instance :Rigidbody = Instantiate(arrow, muzzlePoint.position, muzzlePoint.rotation);
     instance.transform.Translate(vec);
 

}

I don't get what is the problem. I created Vector3 which has x,y and Z coordinates...

x will be ( left or right) y will be ( hight ) z will be ( distance )

Anyways, if someone has better idea of firing arrow's pls copy paste link/script advice.

Thanks for any help 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
0
Best Answer

Answer by fafase · Aug 15, 2012 at 05:26 AM

I guess you could try

 var other:Camera;
 
 function Start(){
     other = FindObjectOfType(Camera);
 }
 
 function Update () { 
 
 if(Input.GetMouseButtonDown(1)){
  var hit : RaycastHit;
     if(Physics.Raycast(other.ScreenPointToRay(Input.mousePosition), hit)){
  var vec:Vector3 = hit.point;
     }
     var instance :Rigidbody = Instantiate(arrow, muzzlePoint.position, muzzlePoint.rotation);
     instance.Movement(vec);
     //instance.rigidbody.AddForce(muzzlePoint.transform.forward*forceApplied);
    // I 'd rather use the rigidbody, looks more natural.    
 }

In a arrow script

 var vect:Vector3;
 function Update(){
    transform.Translate(vect*Time.deltaTime);
 }
 function Movement(v:Vector3){
     vect = v;
 }

You use the ScreenPointToRay with the click of the position of the mouse on the screen to convert to the world. In the muzzle script you call the function from the arrow that sets the vector for the translation. As I showed, you would rather go for a rigidbody that handles physics more easily for you. On top of that your arrow will have a natural parabolic path.

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 Neroner23 · Aug 15, 2012 at 12:40 PM

Thanks fafase, this looks really nice. i tryed adding force to my arrow to fire it from my muzzle point, but when use .forward it goes left, and when i use .down it goes forward. What do u think why ??

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 fafase · Aug 15, 2012 at 12:42 PM 0
Share

For info this should be a comment under my answer. Now your issue, when you select the muzzle object you should have the three red green and red arrows indicating the transform. The blue one indicates forward. If your object has a rotation then that could be the reason why it goes left.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Different between transform.Translate() and Vector3.MoveTowards? 1 Answer

Object keeps moving up even when StopCoroutine is called 2 Answers

Does unity have the capability to produce something along the lines of KOTOR? 2 Answers

How would I be able to make an RPG game? 1 Answer

Enemy Database for RPG Battle System? 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