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 /
This question was closed Mar 20, 2017 at 02:38 AM by revan1611 for the following reason:

Other

avatar image
0
Question by revan1611 · Mar 14, 2017 at 08:47 AM · controllerspace shooterdrag objects

How to add touch drag controls on Space Shooter?

So i've completed Space Shooter tutorial, also i finished tutorial on how to port it on mobile device, everything works fine except touch controls showed in tutorial doesn't fit me well, it's like it emulates joystick and ship sometimes goes out of control. Can someone please hint me some answers on how can i change player controller script to add drag feature please?

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Done_Boundary 
 {
     public float xMin, xMax, zMin, zMax;
 }
 
 public class Done_PlayerController : MonoBehaviour
 {
     public float speed;
     public float tilt;
     public Done_Boundary boundary;
 
     public GameObject shot;
     public Transform shotSpawn;
     public float fireRate;
     public SimpleTouchPad touchPad;
     public SimpleTouchAreaButton areaButton;
     
      
     private float nextFire;
     private Quaternion calibrationQuaternion;
 
     void Start()
     {
         CalibrateAccelerometer();
     }
 
     void Update ()
     {
         if (areaButton.CanFire () && Time.time > nextFire) 
         {
             nextFire = Time.time + fireRate;
             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
             GetComponent<AudioSource>().Play ();
         }
     }
 
     //Used to calibrate the Iput.acceleration input
     void CalibrateAccelerometer()
     {
         Vector3 accelerationSnapshot = Input.acceleration;
         Quaternion rotateQuaternion = Quaternion.FromToRotation(new Vector3(0.0f, 0.0f, -1.0f), accelerationSnapshot);
         calibrationQuaternion = Quaternion.Inverse(rotateQuaternion);
     }
 
     //Get the 'calibrated' value from the Input
     Vector3 FixAcceleration(Vector3 acceleration)
     {
         Vector3 fixedAcceleration = calibrationQuaternion * acceleration;
         return fixedAcceleration;
     }
 
     void FixedUpdate ()
     {
         //        float moveHorizontal = Input.GetAxis ("Horizontal");
         //        float moveVertical = Input.GetAxis ("Vertical");
 
         //        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         //      Vector3 accelerationRaw = Input.acceleration;
         //      Vector3 acceleration = FixAcceleration (accelerationRaw);
         //      Vector3 movement = new Vector3 (acceleration.x, 0.0f, acceleration.y);
         Vector2 direction = touchPad.GetDirection();
         Vector3 movement = new Vector3(direction.x, 0.0f, direction.y);
         GetComponent<Rigidbody>().velocity = movement * speed;
         
         GetComponent<Rigidbody>().position = new Vector3
         (
             Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax), 
             0.0f, 
             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
         );
         
         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
     }
 }
 
Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
2

Answer by RecyclingBen · Mar 15, 2017 at 11:43 AM

If you want to make your ship follow your finger's position you can set up an empty gameobject with a public static Vector3 saying it's position. Then set its position to equal wherever your finger is located, something like using Unity

public static Vector3 finger; public static Vector3 fingerPosition; void Update() { foreach (Touch touch in Input.Touches) { finger = touch.position; fingerPosition = camera.ScreenToWorldPoint(finger); } transform.position = fingerPosition; }

and then set the ship to move towards this position (I'm sure there are problems in my code, it's untested, but I hope you get the gist of what I'm saying.

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 revan1611 · Mar 15, 2017 at 12:28 PM 0
Share

Thanks, I'll definitely try. Basically i just need to assign player to empty gameobject with this example script, right?

Follow this Question

Answers Answers and Comments

95 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

Related Questions

Problem with NewInputSystem version 1.0.0 - preview.7 0 Answers

How to Make a Train like movement controller 0 Answers

Gamepad rumble independent motors 0 Answers

Space shooter tutorial, space ship keep moving and has interia 1 Answer

My bolt isn't respawning in game Its driving me crazy i have been at it for 2 days !! 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