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 VerrucaG · Aug 19, 2013 at 03:36 PM · rotate objectslowing-downsphere raycast

Bullet slowing down while snapping to sphere normals

I'm having an issue where a bullet will slow down and stop after a short period of time while snapping to surface normals of a sphere. Here is the code I am using:

 function Update () {
     transform.Translate(Vector3.forward * Time.deltaTime * bulletSpeed);
     //transform.position += transform.forward * bulletSpeed * Time.deltaTime;
     
     //Gets rotation object info
     var globePos = new Vector3(0,0,0);
     initCastDir = globePos - transform.position;
     var hit: RaycastHit;
     
     if (Physics.Raycast (transform.position, initCastDir, hit))
     {
         Debug.DrawRay (transform.position, initCastDir * 10, Color.blue);
         
         transform.position = hit.point;
     }
     
     
     
 }

I can resolve this if I use this line in the raycast statement:

 transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);

Unfortunately this forces the bullet to travel only upwards and makes it unable to rotate to the transform of the player.

Can anyone shed any light on this?

Comment
Add comment · Show 2
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 robertbu · Aug 19, 2013 at 03:45 PM 0
Share

Your question is puzzling. I'm missing critical information about what you are trying to solve with the line of code of code and why you want the bullet to snap to the normal of the sphere.

avatar image VerrucaG · Aug 19, 2013 at 03:48 PM 0
Share

so basically I want it to travel around the sphere as the hole game is set on one. Basically I want it to retain its initial transform.rotation and continue along the path the player was facing when the bullet was instantiated.

1 Reply

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

Answer by robertbu · Aug 19, 2013 at 04:17 PM

So if I understand correctly, you want the bullet to curve around to follow a sphere. You can do it with a raycast as you have done. The line of code you want is:

 transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;

But I'd be a bit concerned about accuracy. That is, while we see a sphere, the object is really a mesh with triangles. Since it is a sphere you can calculate the vector more directly:

 var globeUp = transform.position - globePos;
 transform.rotation = Quaternion.FromToRotation(transform.up, globeUp) * transform.rotation;
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 VerrucaG · Aug 19, 2013 at 04:26 PM 0
Share

Brilliant. Thats exactly what I needed Roberto. Can you explain what the * transform.rotation does?

avatar image robertbu · Aug 19, 2013 at 05:25 PM 0
Share

what the * transform.rotation does?

It combines rotations. FromToRotation() generates a rotation from the given vector to the vector. You could consider it a relative rotation. The '* transform.rotation' applies that relative rotation to the current rotation of the object.

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

15 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

Related Questions

Is there a local Time.timeScale? 1 Answer

Raycast to Stop a GUI 1 Answer

[Solved]Terrain: Rotatating an object to the hit.normal + manual rotation 1 Answer

Help platform rotating on x-Axis passes through colliders 0 Answers

Rotating an object in a 3D grid 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