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. Dismiss Notice

[SOLVED] How do I set a GameObject's position to a static value?

Discussion in 'Scripting' started by dwarfengine, Aug 3, 2019.

  1. dwarfengine

    dwarfengine

    Joined:
    Aug 3, 2019
    Posts:
    10
    Hey guys. I want to be able to control a GameObject's position with my players current facing direction. I already have the code for getting the current facing direction. The GameObject is attached to player already.

    The thing is, when I set its position via gameObject.transform.position, it starts moving indefinitely. I just want the position to be set to the value it receives.

    For example, when I'm moving up, VerticalDirection value becomes 1. I give this value it to the DirectionArrow using this code in the Update function:

    Code (CSharp):
    1. gameObject.transform.position = new Vector3(HorizontalDirection, VerticalDirection);
    The arrow moves like the character. How do I make it move to the point and make it stay there?

    Sorry for my English, tried my best to explain what I want to do.

    Edit: My bad, using rotation was what I actually needed to do.
     
    Last edited: Aug 6, 2019
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    The code you've shown does explicitly set the object's position. You'll need to share more code / what else could be manipulating the position.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Why is it attached to the player if you don't want it to move where the player moves?
     
  4. dwarfengine

    dwarfengine

    Joined:
    Aug 3, 2019
    Posts:
    10
    I solved it. I used rotation.