Unity2D Top-Down Simple Shooting Script C#
Hi guys,
I've been trying several times on making a very simplistic shooting script for my top-down game. I'm still quite not the best at c# and still somewhat new to it.
Previously I've been trying to have my sprite character's front look to wherever the mouse is pointed and then shoot using the right mouse button.
Would really be helpful if someone can help me out, thank you so much guys.
It's not really clear what you're asking for here. If you want someone to write the script for you, this isn't the place to ask, but if you have some code that you need help with, you should post it. It might help to take a look at the FAQ.
Yeah since my question I've worked on the script for the character to look at the mouse position while the player is turning or just moving, but it's not working at the moment.
public class Player$$anonymous$$ovement2 : $$anonymous$$onoBehaviour {
public float speed;
private Camera cam;
private Transform tran;
private Quaternion targetRotation;
Rigidbody2D rbody;
void Awake () {
rbody = GetComponent<Rigidbody2D> ();
}
void Update () {
Control$$anonymous$$ouse ();
}
void Control$$anonymous$$ouse() {
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint (new Vector3 (mousePos.x, mousePos.y, cam.transform.position.y - transform.position.y));
targetRotation = Quaternion.LookRotation (mousePos - new Vector3 (transform.position.x, 0, transform.position.z));
transform.eulerAngles = Vector3.up * $$anonymous$$athf.$$anonymous$$oveTowardsAngle (transform.eulerAngles.y, targetRotation.eulerAngles.y, targetRotation * Time.deltaTime);
Vector3 input = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
Vector3 motion = input;
motion *= ($$anonymous$$athf.Abs(input.x) == 1 && $$anonymous$$athf.Abs(input.z) == 1)?.7f:1;
motion *= (Input.GetButton("Run"))?speed;
motion += Vector3.up * -8;
}
// Update is called once per frame
void FixedUpdate () {
float x = Input.GetAxis ("Horizontal");
float y = Input.GetAxis ("Vertical");
rbody.velocity = new Vector2 (x * speed, y * speed);
}
Can you explain how the script is supposed to work and describe what it does now?
So essentially the Control$$anonymous$$ouse is trying to get the mouse position in the world by using vector3 (but since I'm using vector2 it may not be right).
Then rotating the target to where the mousePos is whenever the button is called (Input.GetAxisRaw)
But like I said the majority of this stuff might be completely wrong, I've just been researching code inputs about this topic and trying to implement them and see if it'll work.
Answer by spooneystone · Mar 23, 2016 at 10:53 AM
You have declared mousePos at the top of update and put Input.mouseposition but in the next line you change it to screentoworldpoint. Create a new variable to take your screentoworldpoint. Try that!
Your answer
Follow this Question
Related Questions
MissingReferenceException what sshould I do? 0 Answers
2D shooting script fixed camera 2 Answers
Shoot until i release the button, but with a fire ratio 2 Answers
Fire projectile towards mouse position top-down 2D 0 Answers
[Unity2D] Adding Sound to a Prefab 0 Answers