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

Help my player only shoot to the right side :P 2d top down game

Discussion in '2D' started by cyberlarry, Jun 18, 2014.

  1. cyberlarry

    cyberlarry

    Joined:
    May 14, 2014
    Posts:
    12
    Hi everybody i have got that my player can move in 8 directions with his respective animations. Now i only can shoot to one side.

    This is my code, i use 3 codes.

    1
    PLAYERSHOOTING

    using UnityEngine;
    using System.Collections;

    public class Playershooting : MonoBehaviour {

    private Animator anim;
    public Vector3 startPosition;

    void Update()
    {
    // ...

    // 5 - Shooting
    bool shoot = Input.GetButtonDown("Fire1");
    shoot |= Input.GetButtonDown("Fire2");
    // Careful: For Mac users, ctrl + arrow is a bad idea

    if (shoot)
    {
    WeaponScript weapon = GetComponent<WeaponScript>();
    if (weapon != null)

    {
    // false because the player is not an enemy
    weapon.Attack(false);
    }
    }}}

    2 WEAPONSCRIPT

    using UnityEngine;

    /// <summary>
    /// Launch projectile
    /// </summary>
    public class WeaponScript : MonoBehaviour
    {
    //--------------------------------
    // 1 - Designer variables
    //--------------------------------

    /// <summary>
    /// Projectile prefab for shooting
    /// </summary>
    public Transform shotPrefab;

    /// <summary>
    /// Cooldown in seconds between two shots
    /// </summary>
    public float shootingRate = 0.25f;

    //--------------------------------
    // 2 - Cooldown
    //--------------------------------

    private float shootCooldown;

    void Start()
    {
    shootCooldown = 0f;
    }

    void Update()
    {
    if (shootCooldown > 0)
    {
    shootCooldown -= Time.deltaTime;
    }
    }

    //--------------------------------
    // 3 - Shooting from another script
    //--------------------------------

    /// <summary>
    /// Create a new projectile if possible
    /// </summary>
    public void Attack(bool isEnemy)
    {
    if (CanAttack) {
    shootCooldown = shootingRate;

    // Create a new shot
    var shotTransform = Instantiate (shotPrefab) as Transform;

    // Assign position
    shotTransform.position = transform.position;

    // The is enemy property
    ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript> ();
    if (shot != null) {
    shot.isEnemyShot = isEnemy;
    }

    // Make the weapon shot always towards it
    MoveScript move = shotTransform.gameObject.GetComponent<MoveScript> ();
    if (move !=null) {
    move.direction = this.transform.right; // towards in 2D space is the right of the sprite
    }

    }
    }
    /// <summary>
    /// Is the weapon ready to create a new projectile?
    /// </summary>
    public bool CanAttack
    {
    get
    {
    return shootCooldown <= 0f;
    }
    }
    }

    3 MOVESCRIPT

    using UnityEngine;

    /// <summary>
    /// Simply moves the current game object
    /// </summary>
    public class MoveScript : MonoBehaviour
    {
    // 1 - Designer variables

    /// <summary>
    /// Object speed
    /// </summary>
    public Vector2 speed = new Vector2(10, 10);

    /// <summary>
    /// Moving direction
    /// </summary>
    public Vector3 direction = new Vector3(1, 0,1);

    private Vector3 movement;

    void Update()
    {
    // 2 - Movement
    movement = new Vector2(
    speed.x * direction.x,
    speed.y * direction.y);
    }

    void FixedUpdate()
    {
    // Apply movement to the rigidbody
    rigidbody2D.velocity = movement;
    }
    }

    The problem: The player only shoot to the right. I want that the player shoot in 8 directions. :p Can you help me?
     
  2. cyberlarry

    cyberlarry

    Joined:
    May 14, 2014
    Posts:
    12
  3. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    If i understood your code correctly, here is the problem:

    Code (CSharp):
    1. // Make the weapon shot always towards it
    2. MoveScript move = shotTransform.gameObject.GetComponent<MoveScript> ();
    3. if (move !=null) {
    4. move.direction = this.transform.right; // towards in 2D space is the right of the sprite
    5. }
    PS:try to use the "code" tag to post code.
     
  4. cyberlarry

    cyberlarry

    Joined:
    May 14, 2014
    Posts:
    12
    But which is the variable that i have that use? I want that shoot forward. i have try everything.

    I have try transform.up shot up all right. But i want that shoot in front of where is seing.

    Its not so evident. i guess.

    Thank you very much for answer me.
     
  5. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
  6. cyberlarry

    cyberlarry

    Joined:
    May 14, 2014
    Posts:
    12

    Then the player shoot up :p. But it doesnt shoot in any direction. The idea is that the player shoot in front of him. If he is seing to the right then fire right, if him is seing to the left then shoot to the left, the same with up, down and upleft, upright,downleft, downright. Thanks for answer me victor.

    If i put LOCALPOSITION where i put RIGHT then the player shoot in any directions but not in the correct side.
     
    Last edited: Jul 9, 2014
  7. cyberlarry

    cyberlarry

    Joined:
    May 14, 2014
    Posts:
    12
    So difficult is that the player shoot in front of him?
     
  8. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    If u want him to shoot where he is pointing u have to calculate the direction every time.
    Search for some tutorials about this, there are plenty of them on the internet ;)