Search Unity

Issues when firing projectiles using Rigidbody2D

Discussion in '2D' started by Lindbrum, Sep 28, 2019.

  1. Lindbrum

    Lindbrum

    Joined:
    Sep 28, 2019
    Posts:
    5
    Hello everyone!
    I'll make the premise that i'm very new to the Unity world and i've been searching for a working solution the past day.
    So, i am trying to implement the logic behind firing projectiles towards my mouse cursor (mobile touch later on since i'm targeting android) from a fixed origin (the character can't move, only fire). I attached a Rigidbody2D to the projectiles and the script that generate those set the target point after instantiating, after that the rest is left in the hands of the "MoveProjectile" script i have put on the projectile prefab.
    Here is the script code (it's short so i'm posting it fully):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ProjectileMove : MonoBehaviour
    6. {
    7.     public Vector3 target;
    8.     public float speed = 6.0f;
    9.     private Vector2 movement;
    10.     private Rigidbody2D rigidbody;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         rigidbody = GetComponent<Rigidbody2D>();
    16.         var direction = (target - transform.position);
    17.         direction = direction.normalized;
    18.         movement = direction * speed;
    19.     }
    20.  
    21.     void FixedUpdate()
    22.     {
    23.         //GetComponent<Rigidbody2D>().AddForce(projectileInst.transform.forward * 1000);
    24.         rigidbody.MovePosition(rigidbody.position + movement * Time.fixedDeltaTime);
    25.      
    26.     }
    27. }
    Both methods (using AddForce and MovePosition) successfully move the projectiles towards the mouse click location, however the linear velocity of each bullet depends from the distance between spawn origin and mouse click location, as you can see from the following images:
    1) Clicking on the extreme right of the game screen:

    2) Clicking right on top of the spawn origin:


    Debug logs show that the normalized vector shrinks the closer i get to the origin while i was expecting it to preserve the unitary magnitude...
    It feels like i'm missing something but i can't really grasp it...
     
    Last edited: Sep 28, 2019
  2. ZliVuk

    ZliVuk

    Joined:
    Mar 24, 2018
    Posts:
    61
    Try watching scene from 3D view. Maybe the z part of vector is messing something up.
     
    MelvMay and Lindbrum like this.
  3. Lindbrum

    Lindbrum

    Joined:
    Sep 28, 2019
    Posts:
    5
    Turned out it was indeed the Z part that resulted from ScreenPointToWorld (Z = -10, same as camera). Setting it to zero did the trick!
    A rookie mistake, i might say :)
     
    ZliVuk likes this.
  4. ZliVuk

    ZliVuk

    Joined:
    Mar 24, 2018
    Posts:
    61
    Yeah, happens to me also too many times. Try using Vector2 instead of Vector3 wherever is possible.
     
    Lindbrum likes this.