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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Changing A Value When Approaching an Object

Discussion in 'Scripting' started by Zethro05, Sep 4, 2018.

  1. Zethro05

    Zethro05

    Joined:
    Sep 4, 2018
    Posts:
    5
    I was just wondering how would I program a shader to change its speed when it gets closer to an object. I am
    a begginer at programming and would really like to get better at it.
    upload_2018-9-4_17-25-38.png
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Hi Zethro05, if you are a beginner at programming, I would hope that taking on shader coding doesn't scare you away! :) You have picked a fairly tricky area to begin learning. Is there any particular reason why you are starting with shader coding?
     
  3. Zethro05

    Zethro05

    Joined:
    Sep 4, 2018
    Posts:
    5
    It is just because I was given an assignment at school where I could show a science idea in any sort of way and I wanted to make a game. I have used unity before but not extensively. I went into shader programming because this was used in the tutorial I followed to make a wave animation script.

    It would really help if you could help me, but if it is not possible, I understand.
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I have literally just dabbled with shader coding, so I will defer to other experts in this area if you really intend to stay on that path. :)
     
  5. Zethro05

    Zethro05

    Joined:
    Sep 4, 2018
    Posts:
    5
    thx:)
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,186
    You'll need to attach a C# script to the same object, and have that edit the shader's speed variable based on the distance. Shaders can't really read the state of the game - like the distance between objects. They just use the variables, meshes and textures you give it as well as some global data (stencil buffer and such) to draw things to the screen.
     
  7. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    687
    Technically, you could pass the positions of the objects you want to compare for closeness to the shader, and then do (obj1 - obj2).length to get the distance- then do a lerp to map that distacne to your speed. But that would be executed for every vertex or fragment, so it would be better to do those calculations in a c# script and pass that data to your _Speed parameter.
     
  8. Zethro05

    Zethro05

    Joined:
    Sep 4, 2018
    Posts:
    5
    thx:)
     
  9. Zethro05

    Zethro05

    Joined:
    Sep 4, 2018
    Posts:
    5
    thx:)