Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Jonathan Genesis · Dec 27, 2013 at 03:23 AM · rotationvector2shoot

Shooting with new 2D tools top down single stick

alt text

The player uses the left joystick of a controller (Horizontal and Vertical axis so it worked with WASD) to move and the player faces the direction of the moment. I made it so you shoot when pressing ("Fire1") and it only works when you are moving because it gets the axis of the movement of the joystick. This is a problem because I need you to still be able to shoot when you are standing still. The bullet also needs to go at a steady speed. The z rotation is the correct degree the bullet needs to go in but I don't know how to make that work . Take a look at what I have so far. (Using new 2D toolset)

 #pragma strict
 
 var speed : float;    //speed of the player
 var PosH : float;    //value of change in the Horizontal axis of the controller    
 var PosV : float;    //value of change in the Vertical axis of the controller
 var bullet : Rigidbody2D;    //Projectile being shot has a Rigidbody2D component
 var GunPoint : Transform;    //Point at the tip of the gun where the bullet is instantiated
 var bulletspeed : float;    //Constant speed the bullet goes
 
 function FixedUpdate(){        //FixedUpdate
 PosH = (Input.GetAxis("Horizontal"));    //get the float of change of the axis
 PosV = (Input.GetAxis("Vertical"));        //get the float of change of the axis
 
 gameObject.transform.position.x = gameObject.transform.position.x + speed * PosH;    //move the player baced on the speed and change in the axis
 gameObject.transform.position.y = gameObject.transform.position.y + speed * PosV;    //move the player baced on the speed and change in the axis
 gameObject.transform.LookAt(transform.position + Vector3(PosH, PosV, 0.0), Vector3.forward);    //rotate the player on the z axis baced on the vector of the joystick
 gameObject.transform.rotation.y = 0;    //lock the player rotation to stay 2D
 gameObject.transform.rotation.x = 0;    //lock the player rotation to stay 2D
 
 if(Input.GetButtonDown("Fire1")){    //Press button to fire
     var bulletInstance = Instantiate(bullet, GunPoint.position, Quaternion.Euler(Vector3(PosH,PosV,0)));    //instantiate the bullet at the correct position and set rotation to current rotation
     bulletInstance.velocity = Vector2(bulletspeed * PosH, bulletspeed * PosV);        //This is where the problem is cant figure it out
     Destroy(bulletInstance, 1);        //destroy the bullet
     }
 }
screen shot 2013-12-26 at 9.04.42 pm.png (55.3 kB)
Comment
Add comment · Show 2
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 robertbu · Dec 27, 2013 at 03:33 AM 0
Share

The way you are doing your rotation is non-traditional and I'm not completely sure of the ramifications. Plus this is not top down to me since it appears the camera is facing positive 'z' rather than looking down on the 'y' axis. Here is a suggestion I make as a comment since I'm unsure of things:

 var bulletInstance = Instantiate(bullet, GunPoint.position, transform.rotation);   //instantiate the bullet at the correct position and set rotation to current rotation
 bulletInstance.velocity = transform.right * bulletSpeed;

This code assumes that the front is on the right side of your sprite.

avatar image Jonathan Genesis · Dec 28, 2013 at 01:53 AM 0
Share

this would work if I only needed to shoot in one direction. I need to be able to shoot the way the player is facing so right wouldn't work.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KellyThomas · Dec 27, 2013 at 03:35 AM

Well you need to recall the direction from when input was last entered.

This would be one approach:

 var facing: Vector3 = new Vector3(0.0f, 1.0f, 0.0f); //default to facing "up"
 var deadzone: float = 0.1f;


 //then in fixed update
 if ( Mathf.Abs(PosH) > deadzone || Mathf.Abs(PosV) > deadzone) {
     facing = new Vector3(PosH, PosV, 0.0f);
     facing.Normalize();
 }
 if(Input.GetButtonDown("Fire1")){   //Press button to fire
     var bulletInstance = Instantiate(bullet, GunPoint.position, Quaternion.Euler(facing));   //instantiate the bullet at the correct position and set rotation to current rotation
     bulletInstance.velocity = facing * bulletspeed;  //This is where the problem is cant figure it out
     Destroy(bulletInstance, 1);       //destroy the bullet
 }

}

Comment
Add comment · Show 1 · 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
avatar image Jonathan Genesis · Dec 27, 2013 at 07:02 AM 0
Share

Thx for answering so fast it worked GREAT!

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

19 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

Related Questions

Fire Bullet To Mouse Position Problem 2 Answers

Rotate object based on normalized Vector2? 1 Answer

2 fingers sprite rotation 1 Answer

Rotate an object around another object at an angle from the X axis? 1 Answer

Vector rotation 1 Answer


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