The question is answered, right answer was accepted
Throwing a sword
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);
}
}
I put both scripts together (second script begins on 56), sorry for that, and for my english.
Answer by LMan · Jun 24, 2016 at 02:00 PM
Check for input on the button you want to control the sword throwing.
Instantiate the sword prefab with the same rotation as the character
Use Rigidbody2D.AddForce to move the sword object and set the force mode to do what you want- don't set the velocity directly. If you want to skip the physics engine, manipulate transform.position instead of the Rigidbody.
have the sword script check for a collision with enemies, destructables, ect using OnTriggerEnter. Unless you are doing physics things after the sword collision, set the sword collider to be a trigger.
Answer by Mr_Truz · Jun 24, 2016 at 02:52 PM
Ok, I'm throwing swords adding this to my player script:
public Transform firePoint;
public GameObject sword;
void Update () {
if (Input.GetKey (KeyCode.F))
{
Instantiate (sword, firePoint.position, firePoint.rotation);
}
I create an empty object attached to my player (firePoint) and added the prefab sword. When I'm facing right I can shoot a sword to the right buy when I'm facing left the sword also goes to the right from the back of my player. Other question, if I hold down the "F" key (throwing key) never stop throwing swords, I want a "press and release" mechanism, in other words, one push one sword. Any ideas? Thank you.
There's an "add comment" button at the bottom of answers to comment things like this- if you add another answer to ask more questions it just gets confusing.
you can get the direction to move the sword from firePoint.right, assu$$anonymous$$g you want to move the sword in the positive x axis, relative to the firePoint. right now, if you don't change the value of direction, it's going to go the same direction every time.
Have a look at the Input methods to find a method that does what you want. Get$$anonymous$$ey returns true for as long as the button is down. Get$$anonymous$$eyDown returns true only once when the key is pressed.
Follow this Question
Related Questions
Unity 2D 5.2.2 How can I aim with keys towards an object? 2 Answers
How can I program a select level Map like the Super mario bro 3 or Shovel Knight? 0 Answers
I'm making a 2D game with no levels like flappy bird. How many scenes will I need? 1 Answer
Is it possible to add an Input Counter during Splash Screens? 0 Answers
C# 2D top down shooter game, How do I get the player to shoot towards the mouse cursor? 0 Answers