2d Projectile Will Only Move Left

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.

using UnityEngine;
using System.Collections;


public class Player_Projectile : MonoBehaviour {

    public float projectileSpeedRight = 1.0f;
    public float projectileSpeedLeft = 1.0f;
 
    public GameObject playerProjectileRight;
    public GameObject playerProjectileLeft;

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

            if(Input.GetKeyDown ("x"))
            {
                GameObject clone = (GameObject)Instantiate (playerProjectileLeft, transform.position, Quaternion.identity);
                clone.rigidbody2D.velocity = -transform.right * projectileSpeedLeft;
            }

            if(Input.GetKeyDown ("c"))
            {
                GameObject clone2 = (GameObject)Instantiate (playerProjectileRight, transform.position, Quaternion.identity);
                clone2.rigidbody2D.velocity = transform.right * projectileSpeedRight;
            }

}

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.

try Vector3.right. You’re trying to move in local coordinates.

I changed both to Vector3

            if(Input.GetKeyDown ("x"))
            {
                GameObject clone = (GameObject)Instantiate (playerProjectileLeft, transform.position, Quaternion.identity);
                clone.rigidbody2D.velocity = Vector3.left * projectileSpeedLeft;
            }

            if(Input.GetKeyDown ("c"))
            {
                GameObject clone2 = (GameObject)Instantiate (playerProjectileRight, transform.position, Quaternion.identity);
                clone2.rigidbody2D.velocity = Vector3.right * projectileSpeedRight;
            }

same result as before. When I hit c the object instantiates to the left and doesn’t move.

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?

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.

using UnityEngine;
using System.Collections;


public class Player_Projectile : MonoBehaviour {

    public float projectileSpeed = 25.0f;
  
    public GameObject playerProjectile;

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

            if(Input.GetKeyDown ("x"))
            {
            GameObject clone = (GameObject)Instantiate (playerProjectile, transform.position, Quaternion.identity);
            clone.rigidbody2D.velocity = Vector3.left * projectileSpeed;
            }

            if(Input.GetKeyDown ("c"))
            {
            GameObject clone2 = (GameObject)Instantiate (playerProjectile, transform.position, Quaternion.identity);
            clone2.rigidbody2D.velocity = Vector3.right * projectileSpeed;
            }
}

Does your player have a collider attached?

Yeah, a 2d Box Collider

Anyone?

My guess is that your projectile is hitting the player’s box collider and thus not going anywhere.

From the API documentation:

Have you tried to manipulate the gameobjects position?

clone.transform.position = ....

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?

Nope, you can keep your box collider on there.

I could hug you right now… but that would be creepy. Hours of searching and I missed that…

Thanks for the help :smile:

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.