Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved How i can make my laser deflect

Discussion in 'Physics' started by hembrid0123, Nov 25, 2021.

  1. hembrid0123

    hembrid0123

    Joined:
    Sep 1, 2020
    Posts:
    5
    I want to make a Game where a player is holding a Shield that can deflect the Lasers with the Shield but still can be hitted if the Shiled is not hold right.
    I have the Shield as a child object of the player.
    Heres a hand drawn image of how it should work WhatsApp Image 2021-11-25 at 21.35.14.jpeg
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    That's nice and all but what is it you want of anyone here? You don't ask any specific questions about physics (you are on the physics forum after all) and don't say what you've tried, what has failed and what you actually want.

    I suspect this isn't about physics but more of about GameObjects, Rotation etc. If so then a more appropriate forum would be Scripting.

    Maybe state exactly what you want first bearing in mind that nobody is going to write a script for you.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,996
  4. hembrid0123

    hembrid0123

    Joined:
    Sep 1, 2020
    Posts:
    5
    I have knowledge of other things but not this i thought it is physic but i get my first tries with the other reply
    I dont want a script i want to know how it works maybe there is a good tut or a good answer for me, where should i post my thread?
     
  5. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    Just to give a some pointers into the right direction: You will probably want to do Raycasts from the laser source, so read this first: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    All you'll learn, a Raycast "hit" will provide you with a hit point and a normal vector. Use the normal vector together with https://docs.unity3d.com/ScriptReference/Vector3.Reflect.html to find out where the laser beam will go next. You can do that in a loop but don't forget to track the cumulated distance and/or number of bounces and limit both of them, OR ELSE! .. or else you'll potentially run into an infinite loop or a huge workload per frame. This process should give you a list of points that the laser will visit.

    Make heavy use of https://docs.unity3d.com/ScriptReference/Debug.DrawLine.html while you're developing this. Be aware that those debug lines don't show in-game, but only in the scene view, so they're not suitable as a visual laser representation.

    From your illustration I assume that you'd like the laser beam to have some "width" to it. You might think you can use a https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html to simulate the width, but if you really want to stick to your drawing where the laser splits when it partially hits the shield, Sphere casts won't work, because they're just "one thing" that can't be split. So the option you'll end up with is to shoot many rays, each with the same bounce logic as described earlier.

    When all of this is done, you might also want to draw the laser beam. For this, use https://docs.unity3d.com/Manual/class-LineRenderer.html for each of your rays.

    I hope this will get you started.. I can image this might be a fun game where you can place mirrors, you'll have a shield.. maybe it overheats after a while, everything with heavy use of bloom. Good luck!
     
  6. hembrid0123

    hembrid0123

    Joined:
    Sep 1, 2020
    Posts:
    5
    Thank you very much now i can learn all things, its will be a puzzle game where you come to a place and gain a magic shield with that you can proceed the game.
     
    tjmaul likes this.