Unable to have Input.GetButtonDown properly move my GameObject.
Hello,
I've been driving myself crazy trying to get this to work for the last couple of days.
Here's the situation...
I have a cursor GameObject that I move around, I am trying to have the "Submit" button on my controller move another GameObject to the cursor GameObject Transform through Input.GetButtonDown. However, I do not want the movement to be instantaneous, I want the movement to process over a length of time. I have been able to sort of replicate this using Input.GetButton, but I don't want to hold the button down to have the object move to that position.
If it helps to give you a visual, I am trying to have a character walk to the cursor location after pressing a button on my controller.
I've tried MoveTowards, Lerp, Mathf, SmoothDamp, all kinds of things to try and fix this but I have no idea what I'm doing wrong. I've searched all over and have been unable to solve this problem. Any help is greatly appreciated.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BattleMovement : MonoBehaviour {
 
     [SerializeField]
     private float speed;
     [SerializeField]
     private float playerSpeed;
     public Transform target;
     public GameObject MoveCursor;
     public bool isMoving = false;
     private Vector3 finish;
     private Vector3 start;
 
 
     void Update()
     {
         StartCoroutine(PlayerMovement());
     }
 
 
     IEnumerator PlayerMovement()
     {
         yield return new WaitForSeconds(1);
 
         if (Input.GetButtonDown("Submit"))
         {
            start = transform.position;
            BeginMove();
         }
 
     }
 
     void BeginMove()
     {
         if (isMoving == false)
         {
             isMoving = true;
 
             float step = playerSpeed * Time.deltaTime;
 
            // if (isMoving == true)
            //{
            //     transform.position = Vector3.MoveTowards(transform.position, finish, step);
            //}
             transform.position = finish;
 
             if (transform.position == MoveCursor.transform.position)
             {
                 isMoving = false;
             }
 
         }
     }
 
     void FixedUpdate()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 currentVelocity = MoveCursor.GetComponent<Rigidbody2D>().velocity;
 
         float newVelocityX = 0f;
         if (moveHorizontal < 0 && currentVelocity.x <= 0)
         {
             newVelocityX = -speed;
         }
         else if (moveHorizontal > 0 && currentVelocity.x >= 0)
         {
             newVelocityX = speed;
         }
 
         float newVelocityY = 0f;
         if (moveVertical < 0 && currentVelocity.y <= 0)
         {
             newVelocityY = -speed;
         }
         else if (moveVertical > 0 && currentVelocity.y >= 0)
         {
             newVelocityY = speed;
         }
 
         MoveCursor.GetComponent<Rigidbody2D>().velocity = new Vector3(newVelocityX, newVelocityY);
         finish = MoveCursor.transform.position;
     }
 }
Answer by nicholasloire · Nov 27, 2018 at 09:12 PM
I fixed it by changing my code, though I'm still not sure why it didn't work in the first place.
Here's the new code. using System.Collections; using System.Collections.Generic; using UnityEngine;
 public class NewBattleMovement : MonoBehaviour
 {
 
     [SerializeField]
     Transform target;
     float speed = 6f;
     Vector2 targetPos;
     public GameObject MoveCursor;
 
     private void Start()
     {
         targetPos = transform.position;
     }
 
     void Update()
     {
         StartCoroutine(BeginMove());
     }
 
     IEnumerator BeginMove()
     {
         yield return new WaitForSeconds(1);
 
         if (Input.GetButtonDown("Submit"))
         {
             targetPos = MoveCursor.transform.position;
             target.position = targetPos;
         }
         if ((Vector2)transform.position != targetPos)
         {
             transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
         }
     }
 
     void FixedUpdate()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector2 currentVelocity = MoveCursor.GetComponent<Rigidbody2D>().velocity;
 
         float newVelocityX = 0f;
         if (moveHorizontal < 0 && currentVelocity.x <= 0)
         {
             newVelocityX = -speed;
         }
         else if (moveHorizontal > 0 && currentVelocity.x >= 0)
         {
             newVelocityX = speed;
         }
 
         float newVelocityY = 0f;
         if (moveVertical < 0 && currentVelocity.y <= 0)
         {
             newVelocityY = -speed;
         }
         else if (moveVertical > 0 && currentVelocity.y >= 0)
         {
             newVelocityY = speed;
         }
 
         MoveCursor.GetComponent<Rigidbody2D>().velocity = new Vector2(newVelocityX, newVelocityY);
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Mesh Movement 0 Answers
GameObject not looking at me.. 1 Answer
Want to move a character around a map using phones GPS 0 Answers
Making an object follow another object C# 1 Answer
how do i make player die when falling certain height? 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                