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 39 mins ago

























      asked 45 mins ago









      Newbies Studio

      160213




      160213





























          active

          oldest

          votes











          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






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          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

          Feedback on college project

          Futebolista

          Albești (Vaslui)