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 DarthJarvis · Feb 12, 2012 at 04:23 AM · quaternionmathsine-wavepathing

How do I shoot a bullet that follows a sine wave?

Hello!

I am trying to get a "gun" object that fires a "bullet" object in a sine wave pattern. The gun can be pointing in any direction, so to get the bullet to travel forward I use this:

transform.Translate(Vector3.forward * > magnitude * Time.deltaTime);

I want the bullet to always be facing the direction that is moving (always forward), so it's forward direction will rotate as it is moving forward to follow the path of the sine wave. To do this, I think I should rotate the transform. I've tried several ways with no success.

For my last try I did the following:

  Vector3 direction = transform.Forward;
  direction.x += Cos(transform.position.x);
  direction.y += Sin(transform.position.x);
  direction.Normalize();
  transform.rotation = Quaternion.RotateTowards(transform.rotation,
     Quaternion.LookRotation(direction),
     Time.deltaTime);

Then I move it forward using the code I supplied up top, but the bullets just sort of bubble around facing silly directions. I've made so many changes that I don't think the above even makes sense anymore. Can someone help me with the math for this?

Thanks!

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 syclamoth · Feb 12, 2012 at 04:40 AM 0
Share

Try setting the period from the time after firing, not from position.x.

1 Reply

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

Answer by Owen-Reynolds · Feb 12, 2012 at 06:28 PM

The problem with that method is that if you tilt left for a second, then right for a second, sure you'll be aimed in the same direction as when you started, but you'll be to the right of your original path by who knows what. To make all the sideslips cancel, you could have the first tilt right be only 1/2 second. But, once you start tilting by Sins, you need integrals to solve it (if I tilt right faster and faster, then 1/2 second is no longer 1/2 the tilt.)

Having said that, this looks nice and the aim is barely off:

 float startTime;
 Start() { startTime=Time.time; ... } // <--yes, key off time passed

 Update() {
   // tilts up/down (rotate around local x):
   transform.Rotate(Mathf.Sin((Time.time-startTime+1.3f)*6)*1,0,0);
   ...
   // move local forwards here
 }

The *6 is the period: 2PI (6.28) gives exactly one bob/second. The *1 is how steep the bobs are. The 1.3 is a fudge factor. Without it, the bullets tilts up, then down, and ends up facing the way it was fired, but too high, over and over. 1.3 has it about stay even.

To make aiming work, maybe an empty flying the way you shoot, and a child that keys off the correctly aimed, stable parent, but bobbing:

 Transform childBullet;
 float startTime;

 Start() { 
   childBullet = transform.Find("childBulletModel");
   startTime = Time.time;
 }

 FixedUpdate() {
   // wiggle child up/down:
   float Y = Mathf.Sin((Time.time-fireTime)*10)*3;
   childBullet.localPosition = new Vector3(0, Y, 0);

   // horrible hack to aim where we will be in 0.1 seconds:
   Vector3 newPos = transform.position+transform.forward*0.1f+
            transform.up*Mathf.Sin((Time.time+0.1f-fireTime)*10)*3;
   childBullet.LookAt(newPos);
   // move us ...

 As before, `*10` is the period of the wiggle (2PI is 1/sec) and `*3` is the amount, in meters.
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 DarthJarvis · Feb 17, 2012 at 05:10 PM 0
Share

This works fantastically thank you!

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

7 People are following this question.

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

Related Questions

SetFromToRotation vs SetLookRotation 2 Answers

Snapping Connection Points Together Rotation 0 Answers

How do I rotate a vector2d 1 Answer

Comparing quaternion values and extracting values from quaternions 0 Answers

Can anyone clearly explain what Quaternion.angleaxis is 2 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