- Home /
Shoot directions
Hello every one ! (sorry for my bad english) i work with the unity 2d platformer project and i have actually this script for the gun : `using UnityEngine; using System.Collections;
public class Gun : MonoBehaviour { public Rigidbody2D rocket; // Prefab of the rocket. public float speed = 20f; // The speed the rocket will fire at. private bool facingRight = true;
private PlayerControl playerCtrl; // Reference to the PlayerControl script.
private Animator anim; // Reference to the Animator component.
void Awake()
{
// Setting up the references.
anim = transform.root.gameObject.GetComponent<Animator>();
playerCtrl = transform.root.GetComponent<PlayerControl>();
Screen.showCursor = true;
}
void Update ()
{
// If the fire button is pressed...
if(Input.GetButtonDown("Fire1"))
{
// ... set the animator Shoot trigger parameter and play the audioclip.
anim.SetTrigger("Shoot");
audio.Play();
// If the player is facing right...
if(playerCtrl.facingRight)
{
// ... instantiate the rocket facing right and set it's velocity to the right.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(180f,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(speed, 0);
}
else
{
// Otherwise instantiate the rocket facing left and set it's velocity to the left.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(-speed, 0);
}
}
}
}
I want the player shoot in every directions that the mouse point (360 degrees)... I have read a lot of topics about it but i don't understand very well ! Is it someone who can help me with this please ?
Answer by fafase · Apr 21, 2014 at 01:19 PM
So if I got it right, you want to click somewhere on the screen and shoot in the direction from the guy to the click position.
So what you first need is to convert your click from screen to raycast:
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
4th code example
Now the problem is that the raycast needs a collider to stop the ray so you should place an invisible wall, just a collider, right before the player position. So if your player is at z = 0 then place your wall at z = -0.1.
So the method will convert the position of the mouse to world position and throw a ray straight ahead until it hits the wall. The RaycastHit structure will have the position of the hit
hit.point;
now you want to get the direction between the player and that point. we can ignore the z in our case.
Vector3 direction = hit.point - player.transform.position;
direction.z = 0;
Now we have a vector from the player to the hit point. We need to normalize that vector since if we used the vector as is, we would get different result if the click is close or far away.
direction.Normalize();
this keeps the direction but makes the vector of length 1.
You now have all the data to instantiate and shoot your projectile. You don't need to check where the player is facing since it is considered in the vector itself.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(180f,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(direction.x * speed, direction.y*speed);
Thank you for your answer !! I am new in c# and javascript so can you give me the code with all of your stuff because here i don't understand all very well :/ ? Thank you ! ;)
You have already the code, you just need to put in yours. The link has the code to get the click to raycast, simple copy paste, Then add those lines I gave.
Don't get me wrong but if you cannot do that, it may mean you are doing things in the wrong order. You shouldn't think of getting a game done before learning program$$anonymous$$g.
I know I have to learn before but I just test unity for the moment ! ;) All of the lignes of codes I have to put them where in my code precicely ?
The link gives you the condition, then you put your vectors and instantiation inside like you had in the first place.
Answer by pierre53000 · Apr 22, 2014 at 04:34 PM
Why this isn't work ? :/ I have done what you said but it's doesn't work :/ using UnityEngine; using System.Collections;
public class Gun : MonoBehaviour
{
public Rigidbody2D rocket; // Prefab of the rocket.
public float speed = 20f; // The speed the rocket will fire at.
private bool facingRight = true;
private PlayerControl playerCtrl; // Reference to the PlayerControl script.
private Animator anim; // Reference to the Animator component.
void Awake()
{
// Setting up the references.
anim = transform.root.gameObject.GetComponent<Animator>();
playerCtrl = transform.root.GetComponent<PlayerControl>();
}
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, 100))
print("Hit something");
hit.point;
Vector3 direction = hit.point - player.transform.position;
direction.z = 0;
direction.Normalize();
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(180f,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(direction.x * speed, direction.y*speed);
// If the fire button is pressed...
if(Input.GetButtonDown("Fire1"))
{
// ... set the animator Shoot trigger parameter and play the audioclip.
anim.SetTrigger("Shoot");
audio.Play();
// If the player is facing right...
if(playerCtrl.facingRight)
{
// ... instantiate the rocket facing right and set it's velocity to the right.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(speed, 0);
}
else
{
// Otherwise instantiate the rocket facing left and set it's velocity to the left.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(-speed, 0);
}
}
}
}