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 Neralm · Jan 27, 2015 at 06:57 PM · c#2draycastingbounce

ricochet bounce effect issue

First of all im really new to unity and programming, soo expect a loot of noob mistakes :)

Hi , im trying to make a 2D game where the bullet bounces off the walls like a ricochet . I got to the point where i can shoot it to the direction im pointing with the ,, gun '' the way i wanted it , then i attached a RAY to it, pointing towards direction its going to . Now im stuck at the bounce effect , i trying to use the Vector3.reflect but its not bouncing the way i would expect it to bounce .It bounces off almost randomly and sometimes its going though the box colliders . I created a 3d box colliders in the places where the walls are , because i found out that rays don't detect 2d colliders or something like that? . I would like to know if the mistake is in my code or the scene design . I will appreciate every help,tip and criticism :) thanks

This is the code
void Update () {

     Vector3 fwd = transform.TransformDirection(Vector3.up);
     //Debug.DrawRay(raycastObject.transform.position, fwd * raySize, Color.green);

     RaycastHit hit;
     Ray ray = new Ray (transform.position , fwd * raySize ) ;
     Debug.DrawRay (transform.position, fwd * raySize); 

     if (Physics.Raycast (ray , out hit, raySize  )) {
         Debug.Log ("Bouce test 1 ");

                     if (hit.collider.tag == "Wall") {
                             Debug.Log ("Bounce test 2 ");

                             Vector3 reflectDir = Vector3.Reflect (ray.direction , hit.normal);
                             float rot =  Mathf.Atan2 (reflectDir.y, reflectDir.x) * Mathf.Rad2Deg;
                             transform.eulerAngles = new Vector3 (0, 0, rot);

                 }

Also this is the bit im using for movement of the bullet

             Vector3 pos = transform.position; 
             Vector3 velocity = new Vector3 (0, speed * Time.deltaTime, 0); 
             pos += transform.rotation * velocity;
             transform.position = pos; 
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

Answer by Tabemasu Games · Jan 27, 2015 at 07:19 PM

If I were you, I'd give the direction (fwd) to the bullet

 transform.position += fwd * speed * Time.deltaTime;

Then, you just have to change this direction when bouncing:

 myBullet.fwd = Vector3.Reflect (ray.direction , hit.normal);

If you want the bullet to have a rotation which depends on this direction, simply do:

 transform.rotation = Quaternion.LookRotation(fwd);
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 Tabemasu Games · Feb 06, 2015 at 05:03 PM 0
Share

Did this answer help you?

avatar image Neralm · Feb 07, 2015 at 12:53 AM 0
Share

Hi , sorry i didnt reply , i changed some things in the scene , i made the camera look from the top down and it all works perfect :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Flat 2D bounce against gravity? 2 Answers

Enemy bounce from screen edges 0 Answers

Unity 2D Ball Stuck on wall when bouncing 2 Answers

Multiple Cars not working 1 Answer

Cast multiple rays from an object between two angles? 0 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