Auto Move character to specific target instead of keyboard controls Unity3d











up vote
0
down vote

favorite












I want to Auto Move my character to following object, instead of keyboards controls, my code works fine on keyboards controls, I am trying to add a target as a gameobject and trying to character to follow it, "dummy" is variable is my public var, where I assign my target as a gameobject", here is my code so far,



protected virtual void UpdateMovement()
{
// var local = Input.GetAxis("Horizontal") * Vector3.right + Input.GetAxis("Vertical") * Vector3.forward;
//works fine at keyboards controls
var local = new Vector3(0,0,0);
local = dummy.transform.position - transform.position; // I tried this but did'nt work
transform.LookAt(dummy.transform);
var movement = new CharacterMovement();
movement.Direction = getMovementDirection(local);

if (WalkWhenZooming && _controller.ZoomInput)
{
movement.Magnitude = 0.5f;
movement.IsSlowedDown = true;
}
else
{
if ((_motor.ActiveWeapon.Gun != null || _motor.ActiveWeapon.HasMelee) && FastMovement)
{
if (Input.GetButton("Run") && !_motor.IsCrouching)
movement.Magnitude = 2.0f;
else
movement.Magnitude = 1.0f;
}
else
{
if (Input.GetButton("Run"))
movement.Magnitude = 1.0f;
else
movement.Magnitude = 0.5f;
}
}
_controller.MovementInput = movement;
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    I want to Auto Move my character to following object, instead of keyboards controls, my code works fine on keyboards controls, I am trying to add a target as a gameobject and trying to character to follow it, "dummy" is variable is my public var, where I assign my target as a gameobject", here is my code so far,



    protected virtual void UpdateMovement()
    {
    // var local = Input.GetAxis("Horizontal") * Vector3.right + Input.GetAxis("Vertical") * Vector3.forward;
    //works fine at keyboards controls
    var local = new Vector3(0,0,0);
    local = dummy.transform.position - transform.position; // I tried this but did'nt work
    transform.LookAt(dummy.transform);
    var movement = new CharacterMovement();
    movement.Direction = getMovementDirection(local);

    if (WalkWhenZooming && _controller.ZoomInput)
    {
    movement.Magnitude = 0.5f;
    movement.IsSlowedDown = true;
    }
    else
    {
    if ((_motor.ActiveWeapon.Gun != null || _motor.ActiveWeapon.HasMelee) && FastMovement)
    {
    if (Input.GetButton("Run") && !_motor.IsCrouching)
    movement.Magnitude = 2.0f;
    else
    movement.Magnitude = 1.0f;
    }
    else
    {
    if (Input.GetButton("Run"))
    movement.Magnitude = 1.0f;
    else
    movement.Magnitude = 0.5f;
    }
    }
    _controller.MovementInput = movement;
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to Auto Move my character to following object, instead of keyboards controls, my code works fine on keyboards controls, I am trying to add a target as a gameobject and trying to character to follow it, "dummy" is variable is my public var, where I assign my target as a gameobject", here is my code so far,



      protected virtual void UpdateMovement()
      {
      // var local = Input.GetAxis("Horizontal") * Vector3.right + Input.GetAxis("Vertical") * Vector3.forward;
      //works fine at keyboards controls
      var local = new Vector3(0,0,0);
      local = dummy.transform.position - transform.position; // I tried this but did'nt work
      transform.LookAt(dummy.transform);
      var movement = new CharacterMovement();
      movement.Direction = getMovementDirection(local);

      if (WalkWhenZooming && _controller.ZoomInput)
      {
      movement.Magnitude = 0.5f;
      movement.IsSlowedDown = true;
      }
      else
      {
      if ((_motor.ActiveWeapon.Gun != null || _motor.ActiveWeapon.HasMelee) && FastMovement)
      {
      if (Input.GetButton("Run") && !_motor.IsCrouching)
      movement.Magnitude = 2.0f;
      else
      movement.Magnitude = 1.0f;
      }
      else
      {
      if (Input.GetButton("Run"))
      movement.Magnitude = 1.0f;
      else
      movement.Magnitude = 0.5f;
      }
      }
      _controller.MovementInput = movement;
      }









      share|improve this question















      I want to Auto Move my character to following object, instead of keyboards controls, my code works fine on keyboards controls, I am trying to add a target as a gameobject and trying to character to follow it, "dummy" is variable is my public var, where I assign my target as a gameobject", here is my code so far,



      protected virtual void UpdateMovement()
      {
      // var local = Input.GetAxis("Horizontal") * Vector3.right + Input.GetAxis("Vertical") * Vector3.forward;
      //works fine at keyboards controls
      var local = new Vector3(0,0,0);
      local = dummy.transform.position - transform.position; // I tried this but did'nt work
      transform.LookAt(dummy.transform);
      var movement = new CharacterMovement();
      movement.Direction = getMovementDirection(local);

      if (WalkWhenZooming && _controller.ZoomInput)
      {
      movement.Magnitude = 0.5f;
      movement.IsSlowedDown = true;
      }
      else
      {
      if ((_motor.ActiveWeapon.Gun != null || _motor.ActiveWeapon.HasMelee) && FastMovement)
      {
      if (Input.GetButton("Run") && !_motor.IsCrouching)
      movement.Magnitude = 2.0f;
      else
      movement.Magnitude = 1.0f;
      }
      else
      {
      if (Input.GetButton("Run"))
      movement.Magnitude = 1.0f;
      else
      movement.Magnitude = 0.5f;
      }
      }
      _controller.MovementInput = movement;
      }






      c# unity3d






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 12:44









      Basile Perrenoud

      2,98111635




      2,98111635










      asked Nov 19 at 8:03









      Newbies Studio

      162213




      162213
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          It doesn't work doesn't mean much. Is it crashing, showing an error, not moving, moving somewhere else than expected?



          I assume the local variable is a direction (so why not name it direction ?). The way you compute it seems correct. You can simplify the code by replacing



          var local = new Vector3(0,0,0); 
          local = dummy.transform.position - transform.position;


          with



          var local = dummy.transform.position - transform.position;


          If it works with the keyboard, most of your code must be correct. If your character moves way too fast, you can try normalising the direction vector



          local = (dummy.transform.position - transform.position).normalized;


          If it doesn't move or show an error, maybe the dummy isn't set properly. Do you see the expected position if you do



          Debug.Log(dummy.transform.position)


          Edit: You can also try to see if the direction is correct by adding



          Debug.DrawLine(transform.position, transform.position + local)





          share|improve this answer























          • yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
            – Newbies Studio
            Nov 19 at 10:38










          • And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
            – Basile Perrenoud
            Nov 19 at 10:55










          • Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
            – Newbies Studio
            Nov 19 at 12:21










          • but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
            – Newbies Studio
            Nov 19 at 12:22










          • Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
            – Basile Perrenoud
            Nov 19 at 12:49











          Your Answer






          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: "1"
          };
          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',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370521%2fauto-move-character-to-specific-target-instead-of-keyboard-controls-unity3d%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          It doesn't work doesn't mean much. Is it crashing, showing an error, not moving, moving somewhere else than expected?



          I assume the local variable is a direction (so why not name it direction ?). The way you compute it seems correct. You can simplify the code by replacing



          var local = new Vector3(0,0,0); 
          local = dummy.transform.position - transform.position;


          with



          var local = dummy.transform.position - transform.position;


          If it works with the keyboard, most of your code must be correct. If your character moves way too fast, you can try normalising the direction vector



          local = (dummy.transform.position - transform.position).normalized;


          If it doesn't move or show an error, maybe the dummy isn't set properly. Do you see the expected position if you do



          Debug.Log(dummy.transform.position)


          Edit: You can also try to see if the direction is correct by adding



          Debug.DrawLine(transform.position, transform.position + local)





          share|improve this answer























          • yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
            – Newbies Studio
            Nov 19 at 10:38










          • And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
            – Basile Perrenoud
            Nov 19 at 10:55










          • Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
            – Newbies Studio
            Nov 19 at 12:21










          • but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
            – Newbies Studio
            Nov 19 at 12:22










          • Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
            – Basile Perrenoud
            Nov 19 at 12:49















          up vote
          0
          down vote













          It doesn't work doesn't mean much. Is it crashing, showing an error, not moving, moving somewhere else than expected?



          I assume the local variable is a direction (so why not name it direction ?). The way you compute it seems correct. You can simplify the code by replacing



          var local = new Vector3(0,0,0); 
          local = dummy.transform.position - transform.position;


          with



          var local = dummy.transform.position - transform.position;


          If it works with the keyboard, most of your code must be correct. If your character moves way too fast, you can try normalising the direction vector



          local = (dummy.transform.position - transform.position).normalized;


          If it doesn't move or show an error, maybe the dummy isn't set properly. Do you see the expected position if you do



          Debug.Log(dummy.transform.position)


          Edit: You can also try to see if the direction is correct by adding



          Debug.DrawLine(transform.position, transform.position + local)





          share|improve this answer























          • yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
            – Newbies Studio
            Nov 19 at 10:38










          • And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
            – Basile Perrenoud
            Nov 19 at 10:55










          • Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
            – Newbies Studio
            Nov 19 at 12:21










          • but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
            – Newbies Studio
            Nov 19 at 12:22










          • Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
            – Basile Perrenoud
            Nov 19 at 12:49













          up vote
          0
          down vote










          up vote
          0
          down vote









          It doesn't work doesn't mean much. Is it crashing, showing an error, not moving, moving somewhere else than expected?



          I assume the local variable is a direction (so why not name it direction ?). The way you compute it seems correct. You can simplify the code by replacing



          var local = new Vector3(0,0,0); 
          local = dummy.transform.position - transform.position;


          with



          var local = dummy.transform.position - transform.position;


          If it works with the keyboard, most of your code must be correct. If your character moves way too fast, you can try normalising the direction vector



          local = (dummy.transform.position - transform.position).normalized;


          If it doesn't move or show an error, maybe the dummy isn't set properly. Do you see the expected position if you do



          Debug.Log(dummy.transform.position)


          Edit: You can also try to see if the direction is correct by adding



          Debug.DrawLine(transform.position, transform.position + local)





          share|improve this answer














          It doesn't work doesn't mean much. Is it crashing, showing an error, not moving, moving somewhere else than expected?



          I assume the local variable is a direction (so why not name it direction ?). The way you compute it seems correct. You can simplify the code by replacing



          var local = new Vector3(0,0,0); 
          local = dummy.transform.position - transform.position;


          with



          var local = dummy.transform.position - transform.position;


          If it works with the keyboard, most of your code must be correct. If your character moves way too fast, you can try normalising the direction vector



          local = (dummy.transform.position - transform.position).normalized;


          If it doesn't move or show an error, maybe the dummy isn't set properly. Do you see the expected position if you do



          Debug.Log(dummy.transform.position)


          Edit: You can also try to see if the direction is correct by adding



          Debug.DrawLine(transform.position, transform.position + local)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 10:56

























          answered Nov 19 at 9:06









          Basile Perrenoud

          2,98111635




          2,98111635












          • yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
            – Newbies Studio
            Nov 19 at 10:38










          • And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
            – Basile Perrenoud
            Nov 19 at 10:55










          • Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
            – Newbies Studio
            Nov 19 at 12:21










          • but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
            – Newbies Studio
            Nov 19 at 12:22










          • Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
            – Basile Perrenoud
            Nov 19 at 12:49


















          • yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
            – Newbies Studio
            Nov 19 at 10:38










          • And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
            – Basile Perrenoud
            Nov 19 at 10:55










          • Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
            – Newbies Studio
            Nov 19 at 12:21










          • but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
            – Newbies Studio
            Nov 19 at 12:22










          • Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
            – Basile Perrenoud
            Nov 19 at 12:49
















          yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
          – Newbies Studio
          Nov 19 at 10:38




          yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
          – Newbies Studio
          Nov 19 at 10:38












          And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
          – Basile Perrenoud
          Nov 19 at 10:55




          And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
          – Basile Perrenoud
          Nov 19 at 10:55












          Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
          – Newbies Studio
          Nov 19 at 12:21




          Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
          – Newbies Studio
          Nov 19 at 12:21












          but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
          – Newbies Studio
          Nov 19 at 12:22




          but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
          – Newbies Studio
          Nov 19 at 12:22












          Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
          – Basile Perrenoud
          Nov 19 at 12:49




          Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
          – Basile Perrenoud
          Nov 19 at 12:49


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370521%2fauto-move-character-to-specific-target-instead-of-keyboard-controls-unity3d%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

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen