Search Unity

Add Velocity to object twards mouse position at consistent speed

Discussion in '2D' started by czzplnm, Aug 3, 2019.

  1. czzplnm

    czzplnm

    Joined:
    Jul 16, 2019
    Posts:
    41
    I have an issue where, if the mouse is close to the object the speed is slow compared to the mouse being far away from the object the speed is fast.


    I want the object 'launched' twards the mouse position at a consistent speed regardless how close/far from the mouse it is.

    The attached screenshots show the balls moving too fast(balls spread out) when far, and too slow(balls grouped) when close.

    What am I doing wrong here?



    _destination is the mouse position

    Code (CSharp):
    1.    public void GoToDestination(Vector3 _destination)
    2.     {
    3.         //rb.AddRelativeForce(_destination);
    4.         //_destination.y = -_destination.y;
    5.         //_destination = (_destination - transform.position).normalized;
    6.         //rb.velocity = Vector3.Cross(transform.position, _destination) * speed;//(_destination.normalized - transform.position.normalized) * speed;
    7.         //rb.AddForce(_destination * speed);
    8.         //destination = _destination;      
    9.         rb.velocity = (_destination - transform.position).normalized * (speed);
    10.         //destination = _destination;
    11.     }
     

    Attached Files:

  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    can it be, that because of using two vectors3 (different z distance) the ball is moving with the constant speed but you can see it as moving with different speed ? I mean, try to make _destination.z the same as the transform.position.z. Or just try to use vector2: rb.velocity = vector2 _destination - (vector2) transform.position
     
    czzplnm and MelvMay like this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    Yes, be aware that Unity will implicitly convert a Vector3 to a Vector2 so if you are truly using a Rigidbody2D, velocity is a Vector2.

    Try to use Vector2 as soon as possible in your functions to avoid this kind of confusion. For instance, even a mouse-position is a Vector2 not a Vector3.
     
    czzplnm and vakabaka like this.
  4. czzplnm

    czzplnm

    Joined:
    Jul 16, 2019
    Posts:
    41
    Worked! But why does the z affect this?
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    z-p.png

    as example:
    You makes click 2. The z-position from the mouse has the z-position from the camera (-10 by default) (ok, I am not sure about that the z-position from the mouse is the same as camera z-position or zero but the next idea will still explain the different speed of the moving). Now the object is running to the position of the click 2 and going along the b - line. But your monitor just cannot show the change in the z-axe (all 3d games are fakes in the 2d on the screen). Also you can see only the projection from the current way. This will be e- line. Still the real way from the object is b-line: and b = square root (e*e + c*c).

    And as MelvMay said, don't be shy to use vector2 in the 2d or you can get a headache sometimes with the z-position.
     
    Last edited: Aug 3, 2019
    czzplnm likes this.