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 UDN_2f0abe75-257a-49c7-aaed-cdb8b48980dc · Jun 13, 2016 at 10:09 AM · c#mousetouch

Turning Mouse Input into Touch

Hi, So I have this script: using UnityEngine; using System.Collections;

 public class MouseLook : MonoBehaviour {
 
 
 
     public int fireRate;
 
     public float lookSensitivity = 5f;
     public float xRotation;
     public float yRotation;
 
     public Rigidbody bullet;
 
 
     public Transform barrelEnd1;
     public Transform barrelEnd2;
     public Transform barrelEnd3;
     public Transform barrelEnd4;
 
 
 
 
 
 
     void Start () {
 
     }
 
 
 
     void Update() {
         xRotation += Input.GetAxis ("Mouse Y") * lookSensitivity;
         yRotation -= Input.GetAxis ("Mouse X") * lookSensitivity;
 
         xRotation = Mathf.Clamp (xRotation, 0, 0);
 
         transform.rotation = Quaternion.Euler (xRotation, yRotation, 0);
 
 
 
         if (Input.GetMouseButton (0)) {
 
             Rigidbody bulletInstance;
         
 
             bulletInstance = Instantiate (bullet, barrelEnd1.position, barrelEnd1.rotation) as Rigidbody;
             bulletInstance.AddForce (barrelEnd1.forward * fireRate);
 
     
 
             bulletInstance = Instantiate (bullet, barrelEnd2.position, barrelEnd2.rotation) as Rigidbody;
             bulletInstance.AddForce (barrelEnd2.forward * fireRate);
         
 
             bulletInstance = Instantiate (bullet, barrelEnd3.position, barrelEnd3.rotation) as Rigidbody;
             bulletInstance.AddForce (barrelEnd3.forward * fireRate);
 
 
             bulletInstance = Instantiate (bullet, barrelEnd4.position, barrelEnd4.rotation) as Rigidbody;
             bulletInstance.AddForce (barrelEnd4.forward * fireRate);
 
         }
 
 
     
     
     }
 
 
 
 
 }

I would like to make it so that instead of using the mouse position to rotate the player, I want to be able to drag my finger of the screen instead.

Any ideas? @Mmmpies Thanks,

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 Mmmpies · Jun 13, 2016 at 11:39 AM 0
Share

Not got much time to comment at the moment but a quick search on YouTube gives me this...

https://www.youtube.com/watch?v=gGcPQt-oI-E

I think that uses touch for mobile phones, you might want to search for one that uses the 4.6 UI as it'll be a lot easier to implement.

avatar image UDN_2f0abe75-257a-49c7-aaed-cdb8b48980dc Mmmpies · Jun 13, 2016 at 11:46 AM 0
Share

If there is any chance you can change my script when you have the time? It would be so amazing if you could, but only when you have the time of course! @$$anonymous$$mmpies

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Jun 13, 2016 at 08:04 PM

First off do this tutorial https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/panels-panes-windows

You need to start understanding the EventSystem and start geting your hands dirty with some code. That said it's fairly straight forward to convert as you script simply appears to rotate the player. I got rid of all the other stuff for testing and if using the UI like this this script will need to be on a panel (which is why I added the player public variable. If you want a fire button you need to add a button to your canvas and trigger it with an OnClick.

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System.Collections;
 
 public class PointLook : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler {
 
     public GameObject MyPlayer;
 
     private float xRotation;
     private float yRotation;
     private bool pointing = false;
     private Vector2 fingerMouse;
     private float rotationSpeed = 5f;
 
     void Update() {
         if (pointing) {
             yRotation -= (fingerMouse.x / (Screen.width / 2)) * rotationSpeed;
             MyPlayer.transform.rotation = Quaternion.Euler (0, yRotation, 0);
         }
     }
 
     public void OnDrag(PointerEventData ped)
     {
         if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform> (),
                                                 ped.position, ped.pressEventCamera, out fingerMouse))
             return;
     }
 
     public void OnPointerDown(PointerEventData ped)
     {
         if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform> (),
                                                 ped.position, ped.pressEventCamera, out fingerMouse))
             return;
 
         pointing = true;
     }
 
     public void OnPointerUp(PointerEventData ped)
     {
         pointing = false;
     }
 }

I only did this because it interested me though, you really can't expect people to write scripts for you, not even me.

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

174 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

Related Questions

Get Velocity of Rigidbody Object Below Another Object 0 Answers

Shooting a bullet towards the mouse 0 Answers

C# Mathf.Round Not Rounding Input.mouseposition 0 Answers

Problem with simple Character movement on Android (Touch/GetMouseButtonDown) 1 Answer

Touch Force on 3D Object 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