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.
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;
}
}