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

2d Projectile Will Only Move Left

Discussion in 'Scripting' started by ragnor, Dec 16, 2014.

  1. ragnor

    ragnor

    Joined:
    Dec 10, 2013
    Posts:
    10
    Hello everyone, I am making my first game a 2d platformer and I am trying to get both of my attack buttons to work, I want one button (c) to send projectiles left... that is working like a charm. Trying to get the other direction to work (x) has been a real pain, and I don't understand why. The reason I want two different attacks is I plan on having powerups mapable to the different keys (when I get there). But getting the projectiles to work is kind of a priority.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Player_Projectile : MonoBehaviour {
    6.  
    7.     public float projectileSpeedRight = 1.0f;
    8.     public float projectileSpeedLeft = 1.0f;
    9.  
    10.     public GameObject playerProjectileRight;
    11.     public GameObject playerProjectileLeft;
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.  
    16.             if(Input.GetKeyDown ("x"))
    17.             {
    18.                 GameObject clone = (GameObject)Instantiate (playerProjectileLeft, transform.position, Quaternion.identity);
    19.                 clone.rigidbody2D.velocity = -transform.right * projectileSpeedLeft;
    20.             }
    21.  
    22.             if(Input.GetKeyDown ("c"))
    23.             {
    24.                 GameObject clone2 = (GameObject)Instantiate (playerProjectileRight, transform.position, Quaternion.identity);
    25.                 clone2.rigidbody2D.velocity = transform.right * projectileSpeedRight;
    26.             }
    27.  
    28. }
    When I hit c, all it does is create a projectile to the left(in the wrong direction) without any force, it just kinda sits there until the timer runs out and it gets destroyed.
     
  2. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    try Vector3.right. You're trying to move in local coordinates.
     
  3. ragnor

    ragnor

    Joined:
    Dec 10, 2013
    Posts:
    10
    I changed both to Vector3

    Code (CSharp):
    1.  
    2.  
    3.             if(Input.GetKeyDown ("x"))
    4.             {
    5.                 GameObject clone = (GameObject)Instantiate (playerProjectileLeft, transform.position, Quaternion.identity);
    6.                 clone.rigidbody2D.velocity = Vector3.left * projectileSpeedLeft;
    7.             }
    8.  
    9.             if(Input.GetKeyDown ("c"))
    10.             {
    11.                 GameObject clone2 = (GameObject)Instantiate (playerProjectileRight, transform.position, Quaternion.identity);
    12.                 clone2.rigidbody2D.velocity = Vector3.right * projectileSpeedRight;
    13.             }
    same result as before. When I hit c the object instantiates to the left and doesn't move.
     
  4. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    Check your inspector of the object prefab. Its speed may still be set to 1. If so, set both of the speeds to like 10 or 15. Also, if the speed is going to be the same for all projectiles, why not just have one speed variable and control their direction with Vector3.right and Vector3.left?
     
  5. ragnor

    ragnor

    Joined:
    Dec 10, 2013
    Posts:
    10
    This script doesn't go on the object prefab, it goes on the player. The speed is set to 25 in the inspector, has been the whole time. Just changed it in the script, still no change. I went down to one GameObject and float, using the same for both buttons. But still nothing changes.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Player_Projectile : MonoBehaviour {
    6.  
    7.     public float projectileSpeed = 25.0f;
    8.  
    9.     public GameObject playerProjectile;
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.             if(Input.GetKeyDown ("x"))
    15.             {
    16.             GameObject clone = (GameObject)Instantiate (playerProjectile, transform.position, Quaternion.identity);
    17.             clone.rigidbody2D.velocity = Vector3.left * projectileSpeed;
    18.             }
    19.  
    20.             if(Input.GetKeyDown ("c"))
    21.             {
    22.             GameObject clone2 = (GameObject)Instantiate (playerProjectile, transform.position, Quaternion.identity);
    23.             clone2.rigidbody2D.velocity = Vector3.right * projectileSpeed;
    24.             }
    25. }
     
  6. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Does your player have a collider attached?
     
  7. ragnor

    ragnor

    Joined:
    Dec 10, 2013
    Posts:
    10
    Yeah, a 2d Box Collider
     
  8. ragnor

    ragnor

    Joined:
    Dec 10, 2013
    Posts:
    10
    Anyone?
     
  9. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    My guess is that your projectile is hitting the player's box collider and thus not going anywhere.
     
  10. Felandil

    Felandil

    Joined:
    Jul 30, 2013
    Posts:
    32
    From the API documentation:
    Have you tried to manipulate the gameobjects position?
    Code (CSharp):
    1. clone.transform.position = ....
     
  11. ragnor

    ragnor

    Joined:
    Dec 10, 2013
    Posts:
    10
    Yeah, that's what it was. New issue though, how do I handle collisions on my projectile without a box collider? Can I use triggers without a collider?
     
  12. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
  13. ragnor

    ragnor

    Joined:
    Dec 10, 2013
    Posts:
    10
  14. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    You can also set your projectiles to a certain layer and set it to where it won't collide with the Player layer in the physics matrix.