W
W
WalloutDay2015-12-13 16:29:44
C++ / C#
WalloutDay, 2015-12-13 16:29:44

2D game. Script connection. How to call bool from another script?

I have a 2D game. I made shooting and bullets. When you press the shoot button, a bullet with the "moveScript" script is created. But there is a problem, if the character goes to the right, then everything is fine, the bullet flies to the right. And if it goes to the left, then the bullet also goes to the right. There is a directionInput in the script and it is always equal to 1, how can I change it? I think it can be done by calling a character script during the spawn of a bullet, in which we will take the already existing facingright and transform it into an int directionInout variable, so that there is FR (true) then DI = 1, and if false then -1.
I did it but didn't

using UnityEngine;
using System.Collections;

public class MoveScript : MonoBehaviour
{

    public Vector2 speed = new Vector2(10, 10);
    public bool facingRight = true;
    public int directionInput = 1;
    public Vector2 direction = new Vector2(0,0);
    public bool LeftRight;

    private Vector2 movement;

    void Update()
    {

        CharacterController1 LR = GetComponent<CharacterController1>();
        LeftRight = LR.facingRight;
        if (LeftRight == true)
        {
            directionInput = 1;
        }else if (LeftRight == false)
        {
            directionInput = -1;
        }
        movement = new Vector2(speed.x * directionInput, speed.y * direction.y);

    }

    void FixedUpdate()
    {

        GetComponent<Rigidbody2D>().velocity = movement;

        if (directionInput > 0 && !facingRight)
            Flip();
        else if (directionInput < 0 && facingRight)
            Flip();

    }


    void WalkLeft()
    {

        directionInput = -1;

    }

    void WalkRight()
    {
        
        directionInput = 1;

    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }


}
works (what to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WalloutDay, 2015-12-13
@WalloutDay

The problem is that the bullet itself is in the prefab. And perhaps because of this it is impossible to refactor the script.
She is not originally in the game. It appears when pressed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question