Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Stop distance affecting the projectile velocity

Discussion in '2D' started by timdouglas90, Oct 12, 2016.

  1. timdouglas90

    timdouglas90

    Joined:
    Jun 3, 2014
    Posts:
    2
    Hey,

    Just a (hopefully) quick one. I have been struggling to get my mouse position to only really be a direction and have no bearing on the speed of the projectile...to no avail! and also...I've googled this extensively and not really found anything so here is my code...

    Code (CSharp):
    1. void InstantiateProjectile ()
    2.      {
    3.          if(Time.time > nextFire)
    4.          {
    5.              nextFire = Time.time + fireRate;
    6.  
    7.             Instantiate(fireBall, turret.transform.position, turret.transform.rotation);
    8.         }
    9.      }
    The above code is the instantiation...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Projectile : MonoBehaviour {
    5.  
    6.     public float force;
    7.  
    8.     private Rigidbody2D rb2d;
    9.     private float angle;
    10.  
    11.     void Awake ()
    12.     {
    13.         rb2d = GetComponent<Rigidbody2D>();
    14.     }
    15.  
    16.     void Start ()
    17.     {
    18.         Vector3 screenPoint = Camera.main.WorldToScreenPoint(transform.position);
    19.         Vector3 direction = (Input.mousePosition - screenPoint).normalized;
    20.         rb2d.velocity = direction * force;
    21.     }
    22. }
    So my problem is if I click VERY close to the source, the projectiles instantiate and is REALLY slow!

    Hopefully it's just a quick fix!
     
    Last edited: Oct 12, 2016
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    This is probably due to the "direction" vector being too small to normalize.

    One solution would be to always use a local axis as the movement direction, and rotate your projectile to point that axis at the mouse before setting velocity to force * that local axis.

    Just a guess at a possible implementation:
    Code (CSharp):
    1. Vector3 worldMouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    2. worldMouse.z = transform.position.z;
    3. transform.right = worldMouse - transform.position;
    4. rb2d.velocity = transform.right * force;
    As far as I know the local axes will always have a magnitude of 1.

    However, you could also check if your position delta has a magnitude of less than 1, and then multiply by a huge number before normalizing.
     
    Last edited: Oct 12, 2016
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    My guess is that it's actually because you're working with (and normalizing) 3D vectors, even though you're working in 2D, and thinking of it in 2D. So the "direction" vector really does have a length of 1, in 3D space... but most of that direction is in Z, which you then throw out when you assign to rb2d.velocity, leaving you with a very small x and y.

    If you just change your Vector3's to Vector2 in your Start method, I bet the problem will go away.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    ScreenSpace point and Input.mousePosition are both Z=0, effectively vector2's already arent they?
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    I don't think so. WorldToScreenPoint returns the distance of the screen point from the camera. You need to either set that to zero, or assign to a Vector2 (discarding the z value), before you normalize.
     
    LiterallyJeff likes this.