Hey guys, I’m making a 2D RPG for a school class. One of the things I need to do it’s that my player can throw an object (a sword in my “game”), so, I have this two scripts (first the player movement script, second the sword prefab script); When the player is facing right the sword goes right, when is facing left, the sword goes left, how can I make it? Thank you so much.
public class playermovement : MonoBehaviour {
int contador;
public Text Puntuacion;
Rigidbody2D rbody;
Animator anim;
// Use this for initialization
void Start () {
rbody = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
contador = 0;
Puntuacion.text = "o: " + contador;
}
// Update is called once per frame
void Update () {
Vector2 movement_vector = new Vector2 (Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (movement_vector != Vector2.zero) {
anim.SetBool ("iswalking", true);
anim.SetFloat ("input_x", movement_vector.x);
anim.SetFloat ("input_y", movement_vector.y);
} else {
anim.SetBool ("iswalking", false);
}
rbody.MovePosition (rbody.position + movement_vector * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "moneda") {
DestroyObject (other.gameObject);
contador = contador + 1;
Puntuacion.text = "o: " + contador;
}
}
}
public class sword : MonoBehaviour
{
[SerializeField]
private float speed;
private Rigidbody2D myRigidbody;
private Vector2 direction;
// Use this for initialization
void Start () {
myRigidbody = GetComponent<Rigidbody2D> ();
direction = Vector2.right;
}
void FixedUpdate()
{
myRigidbody.velocity = direction * speed;
}
void OnBecameInvisible()
{
Destroy (gameObject);
}
}