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 joreng26 · Mar 12, 2014 at 03:12 PM · raycastbugshooting

Raycast shooting script

Hi, im having a problem with my raycast shooting script, i have this script set up on an empty gameobject at the barrel of the gun. For some reason the script make it spawn bullet holes in really wierd places, got an idea?

 #pragma strict
 var bulletHole : GameObject;
 function Update () {
  var fwd = transform.TransformDirection(Vector3.forward);
  var hit : RaycastHit;
  Debug.DrawRay(transform.position, fwd*10, Color.green);
  
   if(Input.GetButtonDown("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){
      Instantiate(bulletHole) hit.point Quaternion.FromToRotation(Vector3.up, hit.normal);
      }
 }
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 · Mar 12, 2014 at 04:39 PM 0
Share

This code will not compile. Your Instantiate() line is messed up. If this line was fixed, this code should work, but I have to wonder if we are looking at your real code (and therefore your real problem).

avatar image joreng26 · Mar 12, 2014 at 05:02 PM 0
Share

Sorry i posted wrong code, i was looking at someones simular code, my main problem is that bullethole texture wont rotate the right way, it keeps the same rotation if i hit a wall and if i hit a floor

here is my code

 #pragma strict
 var bulletHole : GameObject;
 function Update () {
  var fwd = transform.TransformDirection(Vector3.forward)*10;
  var hit : RaycastHit;
  Debug.DrawRay(transform.position, fwd, Color.green);
  var hitRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
  
   if(Input.GetButtonDown("Fire1") && Physics.Raycast(transform.position, fwd, hit, 1000)){
      Instantiate(bulletHole, hit.point, hitRotation);
      }
   Destroy(bulletHole, 5);
 }
 

1 Reply

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

Answer by robertbu · Mar 12, 2014 at 05:15 PM

Wrong place and wrong rotation are two different questions. This part of the code you originally posted takes care of the alignment:

  Quaternion.FromToRotation(Vector3.up, hit.normal)

This code assumes the bullet hole was on a plane facing up when the rotation is (0,0,0). If you are using a Quad, then substitute Vector3.back.

You can also do it after the instantiate.

   var go = Instantiate(bulletHole, hit.point, Quaternion.identity);
   go.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * go.transform.rotation;

You need to substitute for 'transform.up' whatever side of the object is the face. But the prefab can have an arbitrary rotation, and this code will do the alignment.

Comment
Add comment · Show 8 · 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 joreng26 · Mar 12, 2014 at 05:28 PM 0
Share

Like this?

pragma strict

var bulletHole : GameObject; function Update () { var fwd = transform.TransformDirection(Vector3.forward)*100; var hit : RaycastHit; Debug.DrawRay(transform.position, fwd, Color.green); var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal); var go = Instantiate(bulletHole, hit.point, Quaternion.identity);

   if(Input.GetButtonDown("Fire1") && Physics.Raycast(transform.position, fwd, hit, 100)){
      Instantiate(bulletHole, hit.point, hitRotation); 
      go.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * go.transform.rotation;
      }
   Destroy(bulletHole, 5);
 }
avatar image robertbu · Mar 12, 2014 at 07:52 PM 0
Share

That should work if the natural rotation of your object has the visible side 'up', but there is no reason to pass 'hitRotation' in and then do line 11 as well. Either should work without the other.

avatar image joreng26 · Mar 12, 2014 at 09:05 PM 0
Share

im sorry but still it just keeps the "up" position, it does not rotate sideways

avatar image robertbu · Mar 12, 2014 at 10:26 PM 0
Share

What is the natural orientation of your object? That is, if your bullet hole prefab has a rotation of (0,0,0), what side of the object has the image? Typically it is either back (Quad), or up (Plane). That is what you will need to align with the hit.normal. So in your code above if it is the back side, you will use either Vector3.back or -transform.forward depending on what code above you are using for alignment.

avatar image joreng26 · Mar 13, 2014 at 03:23 PM 0
Share

also with this script it keeps spawning infinitive with bullethole clones

Show more comments

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NEED HELP WITH RAYCAST SHOOTING !!! 1 Answer

Why do I sometimes hit my Collider and sometimes not 1 Answer

This doormanager script from unity 3x Game Development Essentials is supposed to raycast the door open but it doesn't. Why not? Code now attached. 5 Answers

Disable shooting whilst reloading? 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