Search Unity

Resolved Move forward exactly 0.2 unit

Discussion in 'Scripting' started by Corva-Nocta, Oct 4, 2020.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Hey all, need a small bit of code that I am having trouble finding oddly. I need to move my player forward (local forward not worldspace) exactly 0.2 units, I don't need it to be done over time thankfully, an immediate move is better actually. The hard part is also that I never know what Y angle the player is facing, so transform.forward will be necessary. So a "+ Vector3(0.2, 0, 0)" type of code will not work for me.

    I've seen tons of different forms of "Translate.forward * speed" but this doesn't help me much at all since that doesn't give me exact distances. I don't need it to be a physics move either but if that's what I have to do that is ok.

    Any ideas?
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    transform.position += (transform.forward * 0.2f)?

    Edit: transform.localPosition for local space
     
  3. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Yup! That did it! thanks!

    also, weird that this was so hard to find. Google is not my friend on this one
     
    adamgolden likes this.