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 /
avatar image
0
Question by Masamone · Dec 09, 2019 at 06:11 PM · shootingjoystickrotate objectz axis

How can i rotate only Z axis of Player.

Hi! I am making a game like "diep.io" for mobile. I made a movement system with using Joystick system and i want to do it on shooting system,too. I tried but i couldn't. I want make a shooting system with joystick system. I guess i have to rotate the Z axis of player with joystick but i don't know how can i do it. So i need help.

 public Transform firePoint;
 public GameObject bulletPrefab;

 public float bulletForce = 20f;

 void Update()
 {
     if (Input.GetButtonDown("Fire2"))
     {
         Shoot();
     }
 }

 void Shoot()
 {
     RaycastHit2D hitInfo = Physics2D.Raycast(firePoint.position, firePoint.right);

     GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
     Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
     rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);

     if (hitInfo)
     {
         Enemy enemy = hitInfo.transform.GetComponent<Enemy>();
     }
 }

}

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 Fariborzzn · Dec 09, 2019 at 06:36 PM 0
Share

Rotating is always done around axis.. i saw the game you mentioned .. So You CanUse :

 public void LookAt(Transform target);

Take look at it here https://docs.unity3d.com/ScriptReference/Transform.LookAt.html

avatar image Masamone Fariborzzn · Dec 09, 2019 at 06:47 PM 0
Share

Can you tell me in detail?

avatar image Fariborzzn · Dec 09, 2019 at 11:03 PM 0
Share

Sure! In the game you shared the player is rotating to mouse position so give this a try:

  void Update()
     {
     Vector3 mouseposition= Input.$$anonymous$$ousePosition;
     mouseposition=Camera.$$anonymous$$ain.ScreenToWordPoint(mouseposition);
     
     Vector2 DirectionTo$$anonymous$$ousePos=new Vector2(mouseposition.x- 
      transform.position.x,mouseposition.y-transform.position.y);
     
     transform.up=DirectionTo$$anonymous$$ousePos;  //Which axis you want to face 
      mousepos
     }
avatar image Masamone Fariborzzn · Dec 10, 2019 at 02:14 PM 0
Share

Thx but i know it. Just i want do it with joystick, not mouse. I am making a mobile game so i can't use a mouse. But i don't know how can i do it with joystick. Can you help me? public class ShootingJoystick : $$anonymous$$onoBehaviour { public Rigidbody2D rb; public Camera cam; Vector2 movement; Vector2 mousePos;

 private Vector2 moveVelocity;
   


 public Transform player;
 private bool touchStart = false;
 private Vector2 pointA;
 private Vector2 pointB;
 public Transform circle2;
 public Transform outerCircle2;



 
 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 }

 
 void Update()
 {
     Vector3 mouseposition = Input.mousePosition;
     mouseposition = cam.ScreenToWorldPoint(mouseposition);

     Vector2 DirectionTo$$anonymous$$ousePos = new Vector2(mouseposition.x -
      transform.position.x, mouseposition.y - transform.position.y);

     transform.up = DirectionTo$$anonymous$$ousePos;  //Which axis you want to face 



     if (Input.Get$$anonymous$$ouseButtonDown(1))
     {
         pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
         circle2.transform.position = pointA * +1;
         outerCircle2.transform.position = pointA * +1;
         circle2.GetComponent<SpriteRenderer>().enabled = true;
         outerCircle2.GetComponent<SpriteRenderer>().enabled = true;
     }
     if (Input.Get$$anonymous$$ouseButton(1))
     {
         touchStart = true;
         pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
     }
     else
     {
         touchStart = false;
     }
 }

 void FixedUpdate()
 {
     if (touchStart)
     {
         Vector2 offset = pointB - pointA;
         Vector2 direction = Vector2.Clamp$$anonymous$$agnitude(offset, 1.5f);
         

         circle2.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y) * +1;
     }
     else
     {
         circle2.GetComponent<SpriteRenderer>().enabled = false;
         outerCircle2.GetComponent<SpriteRenderer>().enabled = false;
     }
 }

}

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Masamone · Dec 11, 2019 at 03:25 AM

I still need help, can someone help me??

Comment
Add comment · Show 3 · 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 Fariborzzn · Dec 11, 2019 at 07:57 AM 0
Share

Sorry The Title Was "How can i rotate only Z axis of Player" and was thinking about how to implement rotation.. anyway my bad

Did You Settup Your Own Axis? give this a try:

 void Update()
     {
         float HorizontalInput = Input.GetAxis( "Horizontal" ); //Get The Horizontal Axis(x) Of 
          joystick
         transform.eulerAngles+=new Vector3( 0 , 0 , HorizontalInput ); //add rotation
     }

Here It Is

avatar image Masamone Fariborzzn · Dec 11, 2019 at 02:24 PM 0
Share

This time the player's z-axis is changing automatically. I can't control it. I have a ready joystick system but i don't know how can i control the z axis of player with the joystick. I have been stuck in this problem, im trying to solve the problem but i can't. Please help me.

avatar image Fariborzzn Masamone · Dec 11, 2019 at 03:39 PM 0
Share

what do you mean by automatically? i test it and the result is: https://gfycat.com/delayedwellmadeabyssiniangroundhornbill with no problem? do you use standard unity joystick? if you did take look here you should match your joystick with the input system https://www.youtube.com/watch?v=VW58CT27Ls$$anonymous$$

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

121 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 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 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

How do I change this code so that i can press on the joystick more than once to shoot? 1 Answer

GUI Joystick 1 Answer

Joystick Rotation with Transform.RotateAround 0 Answers

How to shoot using the right mobile Joystick? 2 Answers

GetButtonDown doesn't work with Joystick inputs 2 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