gameObject not moving as expected from C# script

I am just starting out with Unity. I have created 4 gameObjects, each has a single sprite attached to it. One of the gameObjects is my player and has its own class, the other 3 are instances of a different class. The player object moves with your finger, using Input.GetTouch. I want the 3 non-player sprites to move away from my player. In theory, this shouldn’t be hard. I’m writing in 2D, so if the x position of the non-player is less than the player’s x position, it should decrease, else it should increase. Same with the y position. But I have been trying to get this to work for 3 days with no luck. The player object moves correctly, if far too fast. But the non-player objects seem to move randomly if at all. Sometimes, only one moves. Here are my classes. Can anyone see what is wrong with my logic?

Player class:

 public Vector2 current;
public Vector2 previous;

// Start is called before the first frame update
void Start()
{
     current = transform.position;
}

// Update is called once per frame
void Update()
{
   
     previous = current;
   
     if (Input.touchCount > 0)
     {
         Touch playerTouch = Input.GetTouch(0);
         if (playerTouch.phase == TouchPhase.Moved)
         {
             current = playerTouch.position;
             current = (Camera.main.ScreenToWorldPoint(current));
             this.transform.position = current * Time.deltaTime;        
         }
     }
}

Non-player class:

    public Vector2 obj_position;
    private Vector2 previous;
    public PlayerBehaviourScript player;


    // Start is called before the first frame update
    void Start()
    {
        obj_position = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        previous = obj_position;
        if(player != null && player.current != player.previous)
        {         
            if (player.current.x < obj_position.x)
            {
                obj_position.x = (obj_position.x + (player.current.x));
            }
            if(player.current.x > obj_position.x)
            {
                obj_position.x = (obj_position.x - (player.current.x));
            }
              
         
            if (player.current.y < obj_position.y)
            {
               obj_position.y = (obj_position.y + (player.current.y));
            }
            if (player.current.y > obj_position.y)
            {
                obj_position.y = (obj_position.y - (player.current.y));
            }
            transform.position = obj_position * Time.deltaTime;
        }
     
    }

You’re supposed to multiply the amount you want to move by Time.deltaTime not the final position like you are doing with these lines.

this.transform.position = current * Time.deltaTime;
transform.position = obj_position * Time.deltaTime;

For example here’s how it would look with the NPC:

obj_position.x = (obj_position.x + player.current.x * Time.deltaTime);

Thanks - I realise I should have included in my post all the things I have tried. What you say was the original iteration, although I put the addition in parentheses before multiplying by the deltaTime.
I tried changing the deltaTime to the whole position (as seen here) when that didn’t work, in case I misunderstood the docs.
I have also tried using RigidBody2D, and removed it again when it made no difference.

I can try putting it back the way it was, but I can’t see why it wouldn’t work before but would start working now. I’ll try it regardless.

Your NPCs are moving based on the “current” Vector2 in your player class, but “current” is not your player’s current position; you are setting the player’s position equal to “current * Time.deltaTime”. This means that the NPCs are moving based on the latest touch position rather than the Player’s position.

Time.deltaTime is just the number of seconds between the last frame and this frame. So this line below is just multiplying the touch position with a very small number, so likely your player will always stay very close to 0,0

transform.position = current * Time.deltaTime;

To move your player, I expect you want something like this:

transform.position = Vector2.MoveTowards(transform.position, current, speed * Time.deltaTime);

Where “speed” is the number of units per second you want your player to be able to move. When you multiply speed by time, it gives you how far the character moves in that period of time. In this case, the period of time is Time.deltaTime because you are running this every Update.

I’m not entirely sure how you want your enemies to move, but you can approach it the same way. Figure out where you want to move and then move in that direction based on the enemy speed.

2 Likes