Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Adrian-Salarda · Mar 22, 2016 at 01:56 AM · unity 2dshootingtop down shootertop-downmouse look

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZefanS · Mar 23, 2016 at 07:49 AM 0
Share

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.

avatar image Adrian-Salarda · Mar 23, 2016 at 09:44 AM 0
Share

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); 

 }
avatar image ZefanS Adrian-Salarda · Mar 23, 2016 at 09:49 AM 0
Share

Can you explain how the script is supposed to work and describe what it does now?

avatar image Adrian-Salarda ZefanS · Mar 23, 2016 at 10:12 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

52 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges