Answer the question
In order to leave comments, you need to log in
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;
}
}
}
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;
}
}
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 (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);
}
Answer the question
In order to leave comments, you need to log in
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;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question