Lock enemy's y-axis when using Vector3.MoveTowards to follow the playerUnity Vector3.MoveTowards over the...

Giving a talk in my old university, how prominently should I tell students my salary?

Why do phishing e-mails use faked e-mail addresses instead of the real one?

I encountered my boss during an on-site interview at another company. Should I bring it up when seeing him next time?

Caulking a corner instead of taping with joint compound?

How to roleplay my character's ethics according to the DM when I don't understand those ethics?

Where is this quote about overcoming the impossible said in "Interstellar"?

Plagiarism of code by other PhD student

Can a space-faring robot still function over a billion years?

Why did the Cray-1 have 8 parity bits per word?

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

Rationale to prefer local variables over instance variables?

Create chunks from an array

Can an earth elemental drown/bury its opponent underground using earth glide?

Being asked to review a paper in conference one has submitted to

Label layer as symbol colors with QGIS 3

In the world of The Matrix, what is "popping"?

Quitting employee has privileged access to critical information

Is there a way to find out the age of climbing ropes?

How can friction do no work in case of pure rolling?

Remove object from array based on array of some property of that object

How do you say “my friend is throwing a party, do you wanna come?” in german

I can't die. Who am I?

Has Wakanda ever accepted refugees?

School performs periodic password audits. Is my password compromised?



Lock enemy's y-axis when using Vector3.MoveTowards to follow the player


Unity Vector3.MoveTowards over the span of a minute with Time.deltaTime2D Lock x Axis on characterVector3.MoveTowards on y axis onlyLock Z axis from the gyroscope rotation [UNITY 3D]How does Vector3.MoveTowards work in Unity?RectTransform Button Start shaking with Vector3.MoveTowards()Only make camera follow player on x axis?Why is Vector3.MoveTowards only working on a single frame?Different animations depending on multiple factors on Unity mechanim animatorHow to completely stop the camera from clipping into the ground













5












$begingroup$


I am using the following code to make a zombie move towards me, but when playing the scene the zombie seems to be moving towards me, but also off the ground and not fixed on the y-axis



 public GameObject ThePlayer;
public float TargetDistance;
public float AllowedRange = 10;
public GameObject TheEnemy;
public float EnemySpeed;
public int AttackTrigger;
public RaycastHit Shot;

void Update()
{
transform.LookAt(ThePlayer.transform);
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange)
{
EnemySpeed = 0.01f;
if (AttackTrigger == 0)
{
//TheEnemy.GetComponent<Animation>().Play("Walking");
transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
}
}
else
{
EnemySpeed = 0;
//TheEnemy.GetComponent<Animation>().Play("Idle");
}
}

if (AttackTrigger == 1)
{
EnemySpeed = 0;
//TheEnemy.GetComponent<Animation>().Play("Attacking");
}
}

void OnTriggerEnter()
{
AttackTrigger = 1;
}

void OnTriggerExit()
{
AttackTrigger = 0;
}


What can i do to ensure that it is sticking to the y axis at all times










share|improve this question









New contributor




Gurdeep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    5












    $begingroup$


    I am using the following code to make a zombie move towards me, but when playing the scene the zombie seems to be moving towards me, but also off the ground and not fixed on the y-axis



     public GameObject ThePlayer;
    public float TargetDistance;
    public float AllowedRange = 10;
    public GameObject TheEnemy;
    public float EnemySpeed;
    public int AttackTrigger;
    public RaycastHit Shot;

    void Update()
    {
    transform.LookAt(ThePlayer.transform);
    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
    {
    TargetDistance = Shot.distance;
    if (TargetDistance < AllowedRange)
    {
    EnemySpeed = 0.01f;
    if (AttackTrigger == 0)
    {
    //TheEnemy.GetComponent<Animation>().Play("Walking");
    transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
    }
    }
    else
    {
    EnemySpeed = 0;
    //TheEnemy.GetComponent<Animation>().Play("Idle");
    }
    }

    if (AttackTrigger == 1)
    {
    EnemySpeed = 0;
    //TheEnemy.GetComponent<Animation>().Play("Attacking");
    }
    }

    void OnTriggerEnter()
    {
    AttackTrigger = 1;
    }

    void OnTriggerExit()
    {
    AttackTrigger = 0;
    }


    What can i do to ensure that it is sticking to the y axis at all times










    share|improve this question









    New contributor




    Gurdeep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      5












      5








      5


      1



      $begingroup$


      I am using the following code to make a zombie move towards me, but when playing the scene the zombie seems to be moving towards me, but also off the ground and not fixed on the y-axis



       public GameObject ThePlayer;
      public float TargetDistance;
      public float AllowedRange = 10;
      public GameObject TheEnemy;
      public float EnemySpeed;
      public int AttackTrigger;
      public RaycastHit Shot;

      void Update()
      {
      transform.LookAt(ThePlayer.transform);
      if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
      {
      TargetDistance = Shot.distance;
      if (TargetDistance < AllowedRange)
      {
      EnemySpeed = 0.01f;
      if (AttackTrigger == 0)
      {
      //TheEnemy.GetComponent<Animation>().Play("Walking");
      transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
      }
      }
      else
      {
      EnemySpeed = 0;
      //TheEnemy.GetComponent<Animation>().Play("Idle");
      }
      }

      if (AttackTrigger == 1)
      {
      EnemySpeed = 0;
      //TheEnemy.GetComponent<Animation>().Play("Attacking");
      }
      }

      void OnTriggerEnter()
      {
      AttackTrigger = 1;
      }

      void OnTriggerExit()
      {
      AttackTrigger = 0;
      }


      What can i do to ensure that it is sticking to the y axis at all times










      share|improve this question









      New contributor




      Gurdeep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I am using the following code to make a zombie move towards me, but when playing the scene the zombie seems to be moving towards me, but also off the ground and not fixed on the y-axis



       public GameObject ThePlayer;
      public float TargetDistance;
      public float AllowedRange = 10;
      public GameObject TheEnemy;
      public float EnemySpeed;
      public int AttackTrigger;
      public RaycastHit Shot;

      void Update()
      {
      transform.LookAt(ThePlayer.transform);
      if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
      {
      TargetDistance = Shot.distance;
      if (TargetDistance < AllowedRange)
      {
      EnemySpeed = 0.01f;
      if (AttackTrigger == 0)
      {
      //TheEnemy.GetComponent<Animation>().Play("Walking");
      transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
      }
      }
      else
      {
      EnemySpeed = 0;
      //TheEnemy.GetComponent<Animation>().Play("Idle");
      }
      }

      if (AttackTrigger == 1)
      {
      EnemySpeed = 0;
      //TheEnemy.GetComponent<Animation>().Play("Attacking");
      }
      }

      void OnTriggerEnter()
      {
      AttackTrigger = 1;
      }

      void OnTriggerExit()
      {
      AttackTrigger = 0;
      }


      What can i do to ensure that it is sticking to the y axis at all times







      unity c#






      share|improve this question









      New contributor




      Gurdeep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Gurdeep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 19 hours ago









      Alex Myers

      2458




      2458






      New contributor




      Gurdeep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 22 hours ago









      GurdeepGurdeep

      261




      261




      New contributor




      Gurdeep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Gurdeep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Gurdeep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          3 Answers
          3






          active

          oldest

          votes


















          7












          $begingroup$

          The reason this is happening is because Vector3.MoveTowards will move all axes toward the target.



          You can specifically exclude changes to the Y axis by creating a new target Vector3 having the Y axis set to the value it should stay at.



          transform.position = Vector3.MoveTowards(
          transform.position,
          new Vector3(
          ThePlayer.transform.position.x,
          transform.position.y,
          ThePlayer.transform.position.z
          ),
          EnemySpeed
          );


          The above code creates a new Vector3 target where the Y axis is the same as the current position instead of the target, preventing Vector3.MoveTowards from changing the Y axis at all.






          share|improve this answer








          New contributor




          EstelS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$













          • $begingroup$
            This works perfectly thank you! But when the zombie gets into close contact with me it seems to lean back and fall into the floor (weird), is it possible to state when it gets within a certain distance of me, stop moving towards me?
            $endgroup$
            – Gurdeep
            21 hours ago






          • 2




            $begingroup$
            Sure, you can use Vector3.Distance(a, b) to check the distance between two objects. docs.unity3d.com/ScriptReference/Vector3.Distance.html
            $endgroup$
            – EstelS
            21 hours ago












          • $begingroup$
            Is there any benefit to this approach over calculating the move-towards vector as normal and then just maintaining the current Y position during the assignment? i.e. Vector3 moveTowards = Vector3.MoveTowards(transform.position, ThePlater.transform.position, EnemySpeed); transform.position = new Vector3(moveTowards.x, transform.position.Y, moveTowards.z);
            $endgroup$
            – Abion47
            14 hours ago



















          4












          $begingroup$

          You can break out the Vector3 target parameter of Vector3.MoveTowards to prevent movement on the y-axis. Simply set the y value of the target position to the same y value as the current position. For example:



          Vector3 currentPosition = transform.position;
          Vector3 targetPosition = new Vector3(ThePlayer.transform.position.x, transform.position.y, ThePlayer.transform.position.z);

          transform.position = Vector3.MoveTowards(currentPosition, targetPosition, EnemySpeed);





          share|improve this answer











          $endgroup$





















            1












            $begingroup$

            The other answers are great. But if you have a RigidBody attached, you can freeze the y rotation via constraints. Then no code change would be necessary.






            share|improve this answer









            $endgroup$









            • 1




              $begingroup$
              Except that the current code is moving the object via transform.position, which completely bypasses the Rigidbody and ignores any locking on its axes.
              $endgroup$
              – DMGregory
              1 hour ago











            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "53"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            Gurdeep is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f168710%2flock-enemys-y-axis-when-using-vector3-movetowards-to-follow-the-player%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            7












            $begingroup$

            The reason this is happening is because Vector3.MoveTowards will move all axes toward the target.



            You can specifically exclude changes to the Y axis by creating a new target Vector3 having the Y axis set to the value it should stay at.



            transform.position = Vector3.MoveTowards(
            transform.position,
            new Vector3(
            ThePlayer.transform.position.x,
            transform.position.y,
            ThePlayer.transform.position.z
            ),
            EnemySpeed
            );


            The above code creates a new Vector3 target where the Y axis is the same as the current position instead of the target, preventing Vector3.MoveTowards from changing the Y axis at all.






            share|improve this answer








            New contributor




            EstelS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $endgroup$













            • $begingroup$
              This works perfectly thank you! But when the zombie gets into close contact with me it seems to lean back and fall into the floor (weird), is it possible to state when it gets within a certain distance of me, stop moving towards me?
              $endgroup$
              – Gurdeep
              21 hours ago






            • 2




              $begingroup$
              Sure, you can use Vector3.Distance(a, b) to check the distance between two objects. docs.unity3d.com/ScriptReference/Vector3.Distance.html
              $endgroup$
              – EstelS
              21 hours ago












            • $begingroup$
              Is there any benefit to this approach over calculating the move-towards vector as normal and then just maintaining the current Y position during the assignment? i.e. Vector3 moveTowards = Vector3.MoveTowards(transform.position, ThePlater.transform.position, EnemySpeed); transform.position = new Vector3(moveTowards.x, transform.position.Y, moveTowards.z);
              $endgroup$
              – Abion47
              14 hours ago
















            7












            $begingroup$

            The reason this is happening is because Vector3.MoveTowards will move all axes toward the target.



            You can specifically exclude changes to the Y axis by creating a new target Vector3 having the Y axis set to the value it should stay at.



            transform.position = Vector3.MoveTowards(
            transform.position,
            new Vector3(
            ThePlayer.transform.position.x,
            transform.position.y,
            ThePlayer.transform.position.z
            ),
            EnemySpeed
            );


            The above code creates a new Vector3 target where the Y axis is the same as the current position instead of the target, preventing Vector3.MoveTowards from changing the Y axis at all.






            share|improve this answer








            New contributor




            EstelS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $endgroup$













            • $begingroup$
              This works perfectly thank you! But when the zombie gets into close contact with me it seems to lean back and fall into the floor (weird), is it possible to state when it gets within a certain distance of me, stop moving towards me?
              $endgroup$
              – Gurdeep
              21 hours ago






            • 2




              $begingroup$
              Sure, you can use Vector3.Distance(a, b) to check the distance between two objects. docs.unity3d.com/ScriptReference/Vector3.Distance.html
              $endgroup$
              – EstelS
              21 hours ago












            • $begingroup$
              Is there any benefit to this approach over calculating the move-towards vector as normal and then just maintaining the current Y position during the assignment? i.e. Vector3 moveTowards = Vector3.MoveTowards(transform.position, ThePlater.transform.position, EnemySpeed); transform.position = new Vector3(moveTowards.x, transform.position.Y, moveTowards.z);
              $endgroup$
              – Abion47
              14 hours ago














            7












            7








            7





            $begingroup$

            The reason this is happening is because Vector3.MoveTowards will move all axes toward the target.



            You can specifically exclude changes to the Y axis by creating a new target Vector3 having the Y axis set to the value it should stay at.



            transform.position = Vector3.MoveTowards(
            transform.position,
            new Vector3(
            ThePlayer.transform.position.x,
            transform.position.y,
            ThePlayer.transform.position.z
            ),
            EnemySpeed
            );


            The above code creates a new Vector3 target where the Y axis is the same as the current position instead of the target, preventing Vector3.MoveTowards from changing the Y axis at all.






            share|improve this answer








            New contributor




            EstelS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $endgroup$



            The reason this is happening is because Vector3.MoveTowards will move all axes toward the target.



            You can specifically exclude changes to the Y axis by creating a new target Vector3 having the Y axis set to the value it should stay at.



            transform.position = Vector3.MoveTowards(
            transform.position,
            new Vector3(
            ThePlayer.transform.position.x,
            transform.position.y,
            ThePlayer.transform.position.z
            ),
            EnemySpeed
            );


            The above code creates a new Vector3 target where the Y axis is the same as the current position instead of the target, preventing Vector3.MoveTowards from changing the Y axis at all.







            share|improve this answer








            New contributor




            EstelS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer






            New contributor




            EstelS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered 21 hours ago









            EstelSEstelS

            711




            711




            New contributor




            EstelS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            EstelS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            EstelS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.












            • $begingroup$
              This works perfectly thank you! But when the zombie gets into close contact with me it seems to lean back and fall into the floor (weird), is it possible to state when it gets within a certain distance of me, stop moving towards me?
              $endgroup$
              – Gurdeep
              21 hours ago






            • 2




              $begingroup$
              Sure, you can use Vector3.Distance(a, b) to check the distance between two objects. docs.unity3d.com/ScriptReference/Vector3.Distance.html
              $endgroup$
              – EstelS
              21 hours ago












            • $begingroup$
              Is there any benefit to this approach over calculating the move-towards vector as normal and then just maintaining the current Y position during the assignment? i.e. Vector3 moveTowards = Vector3.MoveTowards(transform.position, ThePlater.transform.position, EnemySpeed); transform.position = new Vector3(moveTowards.x, transform.position.Y, moveTowards.z);
              $endgroup$
              – Abion47
              14 hours ago


















            • $begingroup$
              This works perfectly thank you! But when the zombie gets into close contact with me it seems to lean back and fall into the floor (weird), is it possible to state when it gets within a certain distance of me, stop moving towards me?
              $endgroup$
              – Gurdeep
              21 hours ago






            • 2




              $begingroup$
              Sure, you can use Vector3.Distance(a, b) to check the distance between two objects. docs.unity3d.com/ScriptReference/Vector3.Distance.html
              $endgroup$
              – EstelS
              21 hours ago












            • $begingroup$
              Is there any benefit to this approach over calculating the move-towards vector as normal and then just maintaining the current Y position during the assignment? i.e. Vector3 moveTowards = Vector3.MoveTowards(transform.position, ThePlater.transform.position, EnemySpeed); transform.position = new Vector3(moveTowards.x, transform.position.Y, moveTowards.z);
              $endgroup$
              – Abion47
              14 hours ago
















            $begingroup$
            This works perfectly thank you! But when the zombie gets into close contact with me it seems to lean back and fall into the floor (weird), is it possible to state when it gets within a certain distance of me, stop moving towards me?
            $endgroup$
            – Gurdeep
            21 hours ago




            $begingroup$
            This works perfectly thank you! But when the zombie gets into close contact with me it seems to lean back and fall into the floor (weird), is it possible to state when it gets within a certain distance of me, stop moving towards me?
            $endgroup$
            – Gurdeep
            21 hours ago




            2




            2




            $begingroup$
            Sure, you can use Vector3.Distance(a, b) to check the distance between two objects. docs.unity3d.com/ScriptReference/Vector3.Distance.html
            $endgroup$
            – EstelS
            21 hours ago






            $begingroup$
            Sure, you can use Vector3.Distance(a, b) to check the distance between two objects. docs.unity3d.com/ScriptReference/Vector3.Distance.html
            $endgroup$
            – EstelS
            21 hours ago














            $begingroup$
            Is there any benefit to this approach over calculating the move-towards vector as normal and then just maintaining the current Y position during the assignment? i.e. Vector3 moveTowards = Vector3.MoveTowards(transform.position, ThePlater.transform.position, EnemySpeed); transform.position = new Vector3(moveTowards.x, transform.position.Y, moveTowards.z);
            $endgroup$
            – Abion47
            14 hours ago




            $begingroup$
            Is there any benefit to this approach over calculating the move-towards vector as normal and then just maintaining the current Y position during the assignment? i.e. Vector3 moveTowards = Vector3.MoveTowards(transform.position, ThePlater.transform.position, EnemySpeed); transform.position = new Vector3(moveTowards.x, transform.position.Y, moveTowards.z);
            $endgroup$
            – Abion47
            14 hours ago













            4












            $begingroup$

            You can break out the Vector3 target parameter of Vector3.MoveTowards to prevent movement on the y-axis. Simply set the y value of the target position to the same y value as the current position. For example:



            Vector3 currentPosition = transform.position;
            Vector3 targetPosition = new Vector3(ThePlayer.transform.position.x, transform.position.y, ThePlayer.transform.position.z);

            transform.position = Vector3.MoveTowards(currentPosition, targetPosition, EnemySpeed);





            share|improve this answer











            $endgroup$


















              4












              $begingroup$

              You can break out the Vector3 target parameter of Vector3.MoveTowards to prevent movement on the y-axis. Simply set the y value of the target position to the same y value as the current position. For example:



              Vector3 currentPosition = transform.position;
              Vector3 targetPosition = new Vector3(ThePlayer.transform.position.x, transform.position.y, ThePlayer.transform.position.z);

              transform.position = Vector3.MoveTowards(currentPosition, targetPosition, EnemySpeed);





              share|improve this answer











              $endgroup$
















                4












                4








                4





                $begingroup$

                You can break out the Vector3 target parameter of Vector3.MoveTowards to prevent movement on the y-axis. Simply set the y value of the target position to the same y value as the current position. For example:



                Vector3 currentPosition = transform.position;
                Vector3 targetPosition = new Vector3(ThePlayer.transform.position.x, transform.position.y, ThePlayer.transform.position.z);

                transform.position = Vector3.MoveTowards(currentPosition, targetPosition, EnemySpeed);





                share|improve this answer











                $endgroup$



                You can break out the Vector3 target parameter of Vector3.MoveTowards to prevent movement on the y-axis. Simply set the y value of the target position to the same y value as the current position. For example:



                Vector3 currentPosition = transform.position;
                Vector3 targetPosition = new Vector3(ThePlayer.transform.position.x, transform.position.y, ThePlayer.transform.position.z);

                transform.position = Vector3.MoveTowards(currentPosition, targetPosition, EnemySpeed);






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 11 hours ago

























                answered 21 hours ago









                Alex MyersAlex Myers

                2458




                2458























                    1












                    $begingroup$

                    The other answers are great. But if you have a RigidBody attached, you can freeze the y rotation via constraints. Then no code change would be necessary.






                    share|improve this answer









                    $endgroup$









                    • 1




                      $begingroup$
                      Except that the current code is moving the object via transform.position, which completely bypasses the Rigidbody and ignores any locking on its axes.
                      $endgroup$
                      – DMGregory
                      1 hour ago
















                    1












                    $begingroup$

                    The other answers are great. But if you have a RigidBody attached, you can freeze the y rotation via constraints. Then no code change would be necessary.






                    share|improve this answer









                    $endgroup$









                    • 1




                      $begingroup$
                      Except that the current code is moving the object via transform.position, which completely bypasses the Rigidbody and ignores any locking on its axes.
                      $endgroup$
                      – DMGregory
                      1 hour ago














                    1












                    1








                    1





                    $begingroup$

                    The other answers are great. But if you have a RigidBody attached, you can freeze the y rotation via constraints. Then no code change would be necessary.






                    share|improve this answer









                    $endgroup$



                    The other answers are great. But if you have a RigidBody attached, you can freeze the y rotation via constraints. Then no code change would be necessary.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 11 hours ago









                    EvorlorEvorlor

                    2,41632567




                    2,41632567








                    • 1




                      $begingroup$
                      Except that the current code is moving the object via transform.position, which completely bypasses the Rigidbody and ignores any locking on its axes.
                      $endgroup$
                      – DMGregory
                      1 hour ago














                    • 1




                      $begingroup$
                      Except that the current code is moving the object via transform.position, which completely bypasses the Rigidbody and ignores any locking on its axes.
                      $endgroup$
                      – DMGregory
                      1 hour ago








                    1




                    1




                    $begingroup$
                    Except that the current code is moving the object via transform.position, which completely bypasses the Rigidbody and ignores any locking on its axes.
                    $endgroup$
                    – DMGregory
                    1 hour ago




                    $begingroup$
                    Except that the current code is moving the object via transform.position, which completely bypasses the Rigidbody and ignores any locking on its axes.
                    $endgroup$
                    – DMGregory
                    1 hour ago










                    Gurdeep is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    Gurdeep is a new contributor. Be nice, and check out our Code of Conduct.













                    Gurdeep is a new contributor. Be nice, and check out our Code of Conduct.












                    Gurdeep is a new contributor. Be nice, and check out our Code of Conduct.
















                    Thanks for contributing an answer to Game Development Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f168710%2flock-enemys-y-axis-when-using-vector3-movetowards-to-follow-the-player%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    El tren de la libertad Índice Antecedentes "Porque yo decido" Desarrollo de la...

                    Castillo d'Acher Características Menú de navegación

                    Connecting two nodes from the same mother node horizontallyTikZ: What EXACTLY does the the |- notation for...