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 Samsterdamn · May 15, 2013 at 07:31 PM · javaweapons

What am I doing wrong

I just tried to convert the following script, http://wiki.unity3d.com/index.php/ShootingWeaponScript

from C# to Java script and I got everything working except for one part and I am not sure what I am doing wrong. Here is the code that I am working with.

 var projecile : Rigidbody;
 var speed = 20;
 var  projMuzzleVelocity : float = 6.0; // in metres per second
 
 var  RateOfFire     : float = 6.0;
 var  Inaccuracy     : float = 6.0;
 var  fireTimer         : float = 6.0;
 
 function Start()
 {
     fireTimer = Time.time + RateOfFire;
 }
 
 function Update () 
 {
     if( Input.GetButtonUp("Fire1"))
     {
         Debug.DrawLine(transform.position, transform.position + transform.forward, Color.red);
         if (Time.time> fireTimer)
         {
             //GameObject.projectile;
             var muzzleVelocity : Vector3 = transform.forward;
             
             if (Inaccuracy != 0)
             {
                 var rand : Vector2 = Random.insideUnitCircle;
                 muzzleVelocity += new Vector3(rand.x, rand.y, 0) * Inaccuracy;
             }
             
             muzzleVelocity = muzzleVelocity.normalized * projMuzzleVelocity;
             //Debug.Log("Fire1");
             clone = Instantiate(projecile, transform.position, transform.rotation);
             
             //clone.velocity = transform.TransformDirection(muzzleVelocity);
             clone.velocity = transform.TransformDirection(Vector3(0,0,speed));
             fireTimer = Time.time + RateOfFire;
             Destroy(clone.gameObject,1.5);
         }
     }
      
 }

So I have tracked the problem to the following line but I am not sure how to make this work.

 clone.velocity = transform.TransformDirection(Vector3(0,0,speed));

If I make the above like this

 clone.velocity = transform.TransformDirection(muzzleVelocity);

everything will work but the player will only shot in one direction. So for example they will always shoot forward, even if they are standing backwards. Like I stated before I am pretty sure that it has something to do with how I am using muzzleVelocity but I am not sure that I am doing wrong.

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

1 Reply

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

Answer by Dave-Carlile · May 15, 2013 at 07:48 PM

You should be able to use transform.forward to get the direction the object is facing.

 clone.velocity = transform.forward * muzzleSpeed;

The forward property is a unit length vector, so you can just multiply that by the speed in order to create the desired velocity vector.

Comment
Add comment · Show 2 · 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 Samsterdamn · May 15, 2013 at 09:05 PM 0
Share

Thanks for the response, I did end up getting it working. by adding the following line of code.

 clone.velocity = muzzleVelocity;

so now everything is working the way that I wanted it to. Thanks for the help.

avatar image robertbu · May 15, 2013 at 09:07 PM 0
Share

@Samsterdamn - if your question is answered, please click on the checkmark next to the answer to close it out.

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

14 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

Related Questions

Picking up and Dropping Weapons 1 Answer

How to add rigidbody with script? 1 Answer

Problems instantiating a prefab (javascript) 1 Answer

[Q] 3d engine for MIPS android platform 0 Answers

How to block a raycast? (Java) 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