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 Schnekorino · Jun 20, 2021 at 09:01 PM · uibuttonmobileshooting

Making an object rotate towards touch

So basically what I am trying to do is make a 2D mobile platformer, controlled with a joystick. I want the player to be able to shoot where he touches while ignoring the buttons and Joystick. I tried for several hours now and now I am out of ideas. UPDATE: I added a Button for shooting that covers the screen, excluding the other button areas. When I test it, it is not accurate and seems to be off sometimes.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityStandardAssets.CrossPlatformInput;
 
 
 public class PlayerShooting : MonoBehaviour
 {
     public float offset;
     
     public GameObject projectile;
     public Transform shotPoint;
     private float timebtwshots;
     public float startTimeBtwShots;
     
    
 
     void Update()
     { 
 
 
         if (timebtwshots <= 0)
         {
             if (CrossPlatformInputManager.GetButtonDown("Shoot"))
             {
                 Instantiate(projectile, shotPoint.position, transform.rotation);
                 timebtwshots = startTimeBtwShots;
                 Vector3 diffecence = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position) - transform.position;
                 float rotZ = Mathf.Atan2(diffecence.y, diffecence.x) * Mathf.Rad2Deg;
                 transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
             }
 
         }
         else
         {
             timebtwshots -= Time.deltaTime;
         }
 
     }
 }
 
 

I am using Unity standard assets and separate Joystick assets. Unity version 2020.3.11f1

This is the script for the instantiated projectile:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ProjectilePlayer : MonoBehaviour
 {
     public float speed;
     public float duration;
     void Start()
     {
         Invoke("DestroyProjectile", duration);
     }
 
     void Update()
     {
         transform.Translate(transform.up * speed * Time.deltaTime);
     }
     void DestroyProjektile()
     {
         Destroy(gameObject);
     }
 }
 
 

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 eneIr · Jun 21, 2021 at 02:12 AM 0
Share

Have you tried using Input.GetTouch() instead of Input.GetMousePosition?

avatar image Schnekorino eneIr · Jun 21, 2021 at 09:12 AM 0
Share

Yes I tried using this but it did not change much

avatar image eneIr Schnekorino · Jun 22, 2021 at 01:29 AM 0
Share

@Schnekorino I mean you can try using it like this:

         Touch lastTouch;
         if(Input.touchCount > 1)
         {
             lastTouch = Input.touches[Input.touchCount - 1];
         }

avatar image Schnekorino · Jun 21, 2021 at 03:03 PM 0
Share

I think a solution could be to somehow exclude the Joystick area from touch detection (I already tried blocking UI touches, but it still rotated towards it, when it was touched

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tinglee82 · Jun 21, 2021 at 01:05 PM

You can try this:

 if (CrossPlatformInputManager.GetButtonDown("Shoot"))
 {
     Instantiate(projectile, shotPoint.position, transform.rotation);
     timebtwshots = startTimeBtwShots;
 
     // Vector3 diffecence = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position) - transform.position;
     // float rotZ = Mathf.Atan2(diffecence.y, diffecence.x) * Mathf.Rad2Deg;
     // transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
 
     // I'm assuming transform.forward is parallel to the direction that your player's "gun" is pointing. Otherwise you can also use transform.right or something.
     var _gunForward = transform.forward
 
     var _newForward = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position) - transform.position;
     
     // we'd wanna to zero the z-direction since you're working on a 2d game
     _newForward = 0;
      
     // you can use quaternion multiplication to chain multiple rotations (sequence matters)
     transform.rotation *= Quaternion.FromToRotation(_gunForward, _newForward);
 
 }

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 Schnekorino · Jun 21, 2021 at 02:50 PM 0
Share

I tried the script but it is not rotating at all

avatar image
0

Answer by eneIr · Jun 26, 2021 at 09:19 AM

@Schnekorino I mean you can try using it like this:

 Touch lastTouch;
          if(Input.touchCount > 1)
          {
              lastTouch = Input.touches[Input.touchCount - 1];
          }

And then use the position of lastTouch for your rotation.

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
avatar image
0

Answer by spilat12 · Jun 26, 2021 at 05:48 PM

The approach I am using for this in my mobile games: have an empty game object as "aim". Whenever you press the screen, its location changes to the location of the touch. The character, however, is looking at the "aim" - that would be a simple LookAt() function! I really like the result, maybe it would suit you as well!

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

263 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 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 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 to make UI Button Fire Continuously ? 3 Answers

How do I effectively use UI buttons to move my character without confusing assets from the asset store? 1 Answer

Shoot when GUI is pressed? 0 Answers

How do I make a "move pad" on the screen? (Mobile) 1 Answer

UI button shows in editor but not on mobile? 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