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 /
avatar image
0
Question by EmpI · Feb 19, 2016 at 05:05 AM · velocitydirectionnormalization

Un-normalize direction

So I have this line of code for when my enemies fire a shot.

 Rigidbody2D Shot;
         Shot = Instantiate (ProjectileTest, target.position, Quaternion.Euler (Vector3.forward)) as Rigidbody2D;
         Shot.velocity = transform.TransformDirection (new Vector3(Random.Range (-firingArc,firingArc),0,1) * bulletspeed);
         Shot.transform.Rotate (Vector3.forward);

The code works exactly the way it should. There's no problems here. Bulletspeed determines how quickly the bullet flies while firingArc gives me control over the bullet spread for when my enemy is a particularly bad shot, for example. target is the bullet spawn position.

However my control over which direction the bullet spawns is lacking, in my opinion. I can only input a number -1 to 1, when I would prefer to use regular Degrees to give me control over my bullet spread, in particular. Are there options for this?

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 Bunny83 · Feb 19, 2016 at 06:44 AM

Well, if you want to use an angle instead of a vector offset you have to use Sin and Cos.

 float angle = Random.Range (-firingArc,firingArc) * Mathf.Deg2Rad;
 Shot.velocity = transform.TransformDirection (new Vector3(Mathf.Sin(angle),0,Mathf.Cos(angle)) * bulletspeed);

In this case "firingArc" should be in degree. So a value of 10 will result in a 20 degree sector since you go left and right.

I'm just a bit confused. You seem to use the x and z component but you use 2D physics. 2D in Unity is usually the x-y plane. Is it a top down game?

Just saw another thing that doesn't make much sense:

 Quaternion.Euler (Vector3.forward)

Quaternion.Euler expects eulerangles in degree. You pass the constant value (0,0,1) so the object will appear almost like not rotated at all (just 1 degree around the y axis).

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 EmpI · Feb 20, 2016 at 12:53 AM 0
Share

It works! Thanks a lot.

To answer your questions yes it is a top-down game. using the X-Z plane is a matter of personal preference as i'd rather not have to worry about things like gravity when I make a rigidbody2D.

As for the Quaternion.Euler, I couldn't tell you. $$anonymous$$ight have been useful once but is now just some artifact-code that needs to be there but isn't really hurting anything.

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

34 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

Related Questions

Calculating the difference between forward direction and velocity direction to set the body roll of my vehicle. 1 Answer

Rigidbody2D velocity in random direction with exceptions 1 Answer

Objects Rotation to modify Objects transform velocity/Direction of travel 1 Answer

Get the direction of an AI Character 1 Answer

velocity direction and reversing it 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