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 DrewCarter101 · Apr 26, 2017 at 10:22 PM · physicsraycastraycastingreflectionray

How to reflect raycast rays of objects

I'm trying to write a script that draws a ray from a sphere in a direction of a specific wall and when that rays hits another wall it reflects of the wall, an if, in the new direction, it hits another wall, it reflects of that too.

This is what I have so far, all it does is draw the ray and draws a new ray if it hits a wall, and it only does it once but I want it to be continuous and the same ray.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;


 public class AI : MonoBehaviour {


 public GameObject wall;
 private Vector3 dir;
 private Vector3 origin;
 private Vector3 fwd;


 // Use this for initialization
 void Start () {

     dir= wall.transform.position - transform.position;
     origin = transform.position;
 }
 
 // Update is called once per frame
 void Update () {

     //laser

     DrawNewRay (origin, dir);

     RaycastHit hit;

     if(Physics.Raycast(origin, dir, out hit))
     {
         if(hit.collider.gameObject.CompareTag("Wall"))
         {
             Vector3 reflectDir = Vector3.Reflect (dir , hit.normal);
             DrawNewRay( hit.point,reflectDir);
         }
     }
 }

 void DrawNewRay(Vector3 originPoint, Vector3 direction){
     fwd = transform.TransformDirection(direction);
     Debug.DrawRay(originPoint, fwd * 50, Color.green);
  }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Duke_Hast_Mich · Apr 27, 2017 at 04:38 PM

You aren't even going to get that in real physics. It may be mostly the same energy packet but it is modified by and at least partially re-emitted by the reflective surface.

However, despite there being no Corona like functions in the Unity API you can have your script re-emit the ray from it's primary hit location and forward it in a new direction with a little calculation. Don't beat your face on the wall looking for API functions that don't exist, at least not here, yet.

This is something else, but it shows the concepts within the code. The code won't straight up work in Unity. You'll have to construct the missing functions from multiple ray casts. https://coronalabs.com/blog/2013/05/07/physics-raycasting-and-reflection/

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 DrewCarter101 · Apr 30, 2017 at 05:37 PM 0
Share

Thank you! it's not exactly what I was looking for but it helps alot

avatar image
0

Answer by PankajOnUnity · Apr 25, 2018 at 05:51 PM

I found solution about it https://youtu.be/xv5K8oIDhwc

Comment
Add comment · 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

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

156 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 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 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 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 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 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 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 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

Best way to get a thickness of a wall between 2 points 2 Answers

Physics.Raycast Not Working... Kinda 0 Answers

Adding MaxDistance to RayCast Stops Raycast From working 0 Answers

How can I spawn an object where I clicked in a 3D space, but spawn it in a 2D location?,How can I spawn objects where I click in a fixed position on 2 axis? 0 Answers

2D raycast not working 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