I have the main_character:
public class MainCharacterMovement : MonoBehaviour
{
public Rigidbody2D projectile;
public float speed = 10;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//GetKeyDown checks if a button is clicked, but GetKey checks if a button is held down.
//LEFT
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.Translate(-1 * Vector2.right);
}
//RIGHT
if (Input.GetKey (KeyCode.RightArrow))
{
transform.Translate(Vector2.right);
}
//UP
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(Vector2.up);
}
//DOWN
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(-1 * Vector2.up);
}
//SPACE BAR (attack)
if(Input.GetKey(KeyCode.Space) && GameObject.Find("weapon_1(Clone)") == null)
{
Vector3 frontPosition = new Vector3(transform.position.x + 2, transform.position.y, transform.position.z);
Rigidbody2D cloneProjectile = (Instantiate(projectile, frontPosition, transform.rotation)) as Rigidbody2D;
cloneProjectile.velocity = transform.TransformDirection(new Vector3(speed, 0, 0));
}
}
}
Then I have the weapon_1 movement:
public class Weapon1Movement : MonoBehaviour
{
private int frame;
public Rigidbody2D ball;
// Use this for initialization
void Start ()
{
frame = 0;
}
// Update is called once per frame
void Update ()
{
frame++;
if (frame >= 10)
{
Destroy(this.gameObject);
}
}
}
I have 2 issues:
- When the space bar is hit to create the project (weapon_1), the main_character is pushed back. I’m not sure why this could be.
- When the main_character is moving up/down and shoots at the same time, it creates an angle for the main_character sprite. Then it keeps moving. I’m not sure why this is either.
Here is Inspector tab for the weapon_1 sprite:
Please let me know if there is anything else you need. I’m a Unity newbie.