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.

Question Change gameobject scale based on how close player is to the gameobject?

Discussion in 'Scripting' started by Nsingh13, Jan 12, 2021.

  1. Nsingh13

    Nsingh13

    Joined:
    Dec 17, 2014
    Posts:
    36
    I need an algorithm for this for my game and I can't seem to figure it out. When my player gets closer to the gameobject, the size of the object should decrease. When the player gets further away, the size of the object should increase. In the end, what I am actually trying to do is have it so that the gameobject is always touching the player (by getting larger or smaller).

    BONUS: If I can also make the gameobject rotate to always face the player that would be a plus.
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,584
    I would calculate the vector3.distance between the player and the gameobject then increase/decrease the scale based on the number you get. It would take some tweaking after that but should put you on the right path. As for making the gameobject always rotate towards the player that's actually quite easy, use transform.lookat .
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,353
    Are you trying to achieve an effect like this?

     
    seejayjames likes this.
  4. Nsingh13

    Nsingh13

    Joined:
    Dec 17, 2014
    Posts:
    36
    Thanks. I'll try this tonight.
     
  5. Nsingh13

    Nsingh13

    Joined:
    Dec 17, 2014
    Posts:
    36
    Not exactly, but that is really cool!