Y
Y
Yura Berezovsky2017-03-24 21:19:40
Game development
Yura Berezovsky, 2017-03-24 21:19:40

How to change the direction of the bullet?

Recently started working in Unity. I am doing a simple project. During the development process, the question arose how to change the direction of the bullet? She always flies to the right. I tried many different functions and the result is the same

using UnityEngine;
using System.Collections;
//Код для создания
public class ShootScript : MonoBehaviour
{


    private PlayerControl playerControl;
    public Transform sword;
    

    public float shootingRate = 0.25f;

    public bool isEnemy = true;

    private float shootCooldown;
    private MoveScript move;

    public float fireRate = 0.5f;
    private float nextFire = 1f;
    void Start()
    {
        
        playerControl = GetComponent<PlayerControl>();
       
        move = GetComponent<MoveScript>();
    }

    void Update()
    {
        
        if (nextFire > 1)
        {
            nextFire -= Time.deltaTime;
        }
        
    }
    public void Attack(bool isEnemy)
    {
       
            if (CanAttack && Time.time>nextFire)
            {

            nextFire = Time.time + fireRate;
            var shootTransform = Instantiate(sword) as Transform;


                shootTransform.position = transform.position;


                MoveScript move = shootTransform.gameObject.GetComponent<MoveScript>();
                if (move != null)
                {



                    move.direction = this.transform.right;


                }
            }
        
    }


    public bool CanAttack
    {
        get
        {
            return shootCooldown <= 0f;
            
        }
    }
}

Code for the bullet itself
using UnityEngine;
using System.Collections;

public class MoveScript : MonoBehaviour
{
  public Vector2 speed = new Vector2(80, 80);
  public Vector2 direction = new Vector2(0, 0);
  
  private Vector2 movement;
  private Rigidbody2D rigidbodyComponent;
    private PlayerControl player;
  void Start()
    {
        player = GetComponent<PlayerControl>();
    }
  void Update()
  {
      

           movement = new Vector2(
           speed.x * direction.x * 1,
           speed.y * direction.y * 1);
    }
  void OnCollisionEnter2D(Collision2D  collision) {
    if (collision.transform.tag == "Ground" || collision.transform.tag == "Side") {
      speed = new Vector2(0,0);

    }

  }
   

    void FixedUpdate()
  {
    if (rigidbodyComponent == null) rigidbodyComponent = GetComponent<Rigidbody2D>();
    

    rigidbodyComponent.velocity = movement;
  }
}

Bullet Creation Code
using UnityEngine;
using System.Collections;

public class WeaponControlScript : MonoBehaviour {
    public KeyCode Fire = KeyCode.F;
    private ShootScript shootScript;
    private PlayerControl player;
    // Use this for initialization
    void Start () {
        player = GetComponent<PlayerControl>();
        shootScript = GetComponent<ShootScript>();
    }
  
  // Update is called once per frame
  void Update () {
        
        
            if (Input.GetKeyDown(Fire))
            {
                shootScript.Attack(true);

            }
        
  }
}

If in MoveScript change to
if (player.isFacingRight == true)
        {
            movement = new Vector2(
            speed.x * direction.x * 1,
            speed.y * direction.y * 1);
        } else if  (player.isFacingRight == false)
            {
                movement = new Vector2(
                speed.x * direction.x * -1,
                speed.y * direction.y * 1);
            }

The bullet just falls. I need to make it so that the bullet is in the direction where the cursor was when the fire button was pressed. 3766ac252402467eba0f8bc8d2d490e7.png5ba6eca4c39f48b0bc6502a57cd68a1b.pngHow to change the direction of the bullet? She always flies to the right.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Basmanov, 2017-03-25
@Jurajkeee

The bullet flies to the right, because inside the ShootScript it is given this direction:

MoveScript move = shootTransform.gameObject.GetComponent<MoveScript>();
if (move != null)
{
    move.direction = this.transform.right;
}

Ask another - will fly in the other direction. The desired vector can be found using Input.mousePosition and Camera.ScreenToWorldPoint .
In general, especially for such cases, the unit creates tutorial projects , where there are ready-made scripts that you can study and do your own, or use as is.

A
Alexej Simakov, 2017-03-24
@lxsmkv

https://love2d.org/wiki/Tutorial:Fire_Toward_Mouse

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question