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 /
  • Help Room /
avatar image
0
Question by S.John · Jan 11, 2016 at 03:52 PM · linerendererraycasthit2daimingreflect

problem reflecting a ray

Hello,

I am having trouble reflecting a ray for a simple aiming system (ike a laser with one bounce on a rigidbody, no physic calculations or what so ever).

I kinda have it working but the angle is off. I am struggling with this for a day now so i think i need some extra eyes!

 using UnityEngine;
 using System.Collections;
 
 public class AimBeam : MonoBehaviour {
 
     public Transform ball;
     public LineRenderer lineRenderer;
 
     public float rayLength;
     public Vector3 reflectDirection;
 
     void Start () {
         ball = GameObject.Find("Ball").transform;
         lineRenderer = this.GetComponent<LineRenderer>();
     }
 
     void Update () {
         //Face Beam In Ball Direction
         transform.rotation = ball.rotation;
 
         //Draw Ray
         Vector2 rayDir = transform.TransformDirection (Vector2.right) * rayLength;
         RaycastHit2D hit = Physics2D.Raycast (transform.position, rayDir);
         Debug.DrawRay (transform.position, rayDir, Color.green);
 
         //Set LineRender Pos 0 & 1
         lineRenderer.SetPosition (0, this.transform.position);
         lineRenderer.SetPosition (1, rayDir);
 
 
 
         //CHeck if Ray hits Collider
         if (hit.collider) {
 
             Vector3 hitPoint = hit.point; 
             lineRenderer.SetPosition (1, hitPoint);
 
             //Draw Normal line
             Debug.DrawRay (hit.point, hit.normal*3, Color.blue);
 
             //Calculate reflect direction
             reflectDirection = Vector3.Reflect (rayDir, hit.normal);
             Debug.Log (reflectDirection);
 
             //Set reflect direction in lineRenderer
             lineRenderer.SetPosition (2, reflectDirection);
 
 
         } else {
             lineRenderer.SetPosition (1, rayDir);
         }
     }
 }

The problem has to be somewhere in this line "reflectDirection = Vector3.Reflect (rayDir, hit.normal);" What am i doing wrong? it has to be something with the input direction i think but i can't figure it out.

Thanks in advanced.

Sr.J.

alt text alt text

schermafbeelding-2016-01-09-om-171600.png (155.4 kB)
schermafbeelding-2016-01-09-om-171607.png (155.2 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

2D Raycast not colliding, but only some of the time... 0 Answers

How do I make a laser that bounces around a room? 0 Answers

Raycast2d does not always work properly 0 Answers

Need help ironing out this weird error in Raycast2D and Vector2.Reflect, for a billiard game test 1 Answer

Vector2.Reflect returns opposite reflection 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