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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with adding current player velocity/moving direction to shot projectile

Discussion in '2D' started by JRissa, Mar 20, 2015.

  1. JRissa

    JRissa

    Joined:
    Mar 20, 2015
    Posts:
    12
    Hi.
    I'm very new to programming +Unity and I would need some help with some basic stuff.
    I'm currently trying to get players current movement taken to equation with shot projectiles in space shooting game. Currently shot projectile starts with set speed, no matter how fast and what direction player ship is moving. This causes projectiles to move slower than ship when shot while moving.

    I've spent hours googling for solution, but I just can't get this to work. This was closest thread I could find to get my problem solved, but couldn't modify code so it would work: http://answers.unity3d.com/questions/802927/adding-a-spaceships-speed-and-direction-to-bullet.html



    I would really appreciate if someone could help me get this to work :)
    Here's the basic code I'm using for shooting:

    using UnityEngine;
    using System.Collections;

    public class Shoot : MonoBehaviour {

    public Rigidbody2D projectile;
    public float speed = 5;



    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    if (Input.GetButtonDown("Fire1"))
    {
    Rigidbody2D instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation)as Rigidbody2D;

    instantiatedProjectile.velocity = transform.TransformDirection(Vector2.up * speed);

    }
    }
    }
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    I've not done much with physics in that regard, but I believe you will want to use AddForce() rather than setting the velocity directly.
     
  3. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    Actually I think using velocity is much easier in this situation. It would be a bit more difficult to calculate the right amount of force. With velocity, it's just a matter of adding the velocity of the player to the desired velocity of the projectile. Something like the below works if the x axis of the projectile is pointed in the direction you want it to go...

    Code (CSharp):
    1.  
    2. Vector2 v = rigidBody.GetRelativeVector(Vector2.right * velocity);
    3. v += shooterRB.velocity;
    4. rigidBody.velocity = v;
    5.  
    where "shooterRB" is the rigidbody of the player, and "velocity" is the base speed you want to give to the projectile. This might not be perfect, but it seems to work well enough.
     
  4. JRissa

    JRissa

    Joined:
    Mar 20, 2015
    Posts:
    12
    Thank you for fast reply. Unfortunately I'm so new to programming that I couldn't figure out how to modify my current code according to your instructions. But I found way to do it:

    Unfortunately this only adds speed, not lower. So if ship is moving backwards/sideways, it only gives more speed to projectile.
     

    Attached Files:

  5. Effervescent

    Effervescent

    Joined:
    Mar 7, 2015
    Posts:
    31
    I think the problem with your code is that you are multiplying the velocity of the projectile by the speed (velocity.magnitude) of the ship. The speed, or magnitude of a vector, is by definition directionless (hence always positive). I have never tried this before but it probably would work if you just remove .magnitude.

    For future reference, it would be easier for us to read and comment if you insert your code as text surrounded by the code tags [cod e=CSharp][/code ] (without the space)!
     
  6. JRissa

    JRissa

    Joined:
    Mar 20, 2015
    Posts:
    12
    Thank you for help, but unfortunately this gives following errors:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Shoot : MonoBehaviour {
    6.  
    7.     public Rigidbody2D projectile;
    8.     public float speed = 5;
    9.  
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.  
    20.         if (Input.GetButtonDown("Fire1"))
    21.         {
    22.             Rigidbody2D instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation)as Rigidbody2D;
    23.          
    24.             instantiatedProjectile.velocity = transform.TransformDirection(Vector2.up * (speed + GetComponent<Rigidbody2D>().velocity));
    25.  
    26.          
    27.         }
    28.     }
    29. }
    30. [/code ]
     

    Attached Files:

    Last edited: Mar 21, 2015
  7. JRissa

    JRissa

    Joined:
    Mar 20, 2015
    Posts:
    12
    As I'm very new to programming; could you please help me to implement this to current code?

    I tried, but failed...

    Thank you :)
     
  8. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    Something like this...


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Shoot : MonoBehaviour {
    4.     public Rigidbody2D projectile;
    5.     public float speed = 5;
    6.  
    7.     Rigidbody2D rigidBody;
    8.     // Use this for initialization
    9.     void Start () {
    10.         rigidBody = GetComponent<Rigidbody2D>();
    11.     }
    12.     // Update is called once per frame
    13.     void Update () {
    14.         if (Input.GetButtonDown("Fire1"))
    15.         {          
    16.  
    17.             Rigidbody2D instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation)as Rigidbody2D;
    18.             Vector2 v = instantiatedProjectile.GetRelativeVector(Vector2.right * speed);
    19.             v += rigidBody.velocity;
    20.             instantiatedProjectile.velocity = v;        
    21.         }
    22.     }
    23. }
     
    Zondi and RoyalCoder like this.
  9. JRissa

    JRissa

    Joined:
    Mar 20, 2015
    Posts:
    12
    Excellent!
    Thank you very much! This did the trick :)