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 marinetech79 · Feb 23, 2014 at 10:13 AM · touchspaceshootertouches

How can i change the player controls in Space Shooter to Touch.

Hello, I am new to coding and looking for some help on trying to convert the Space Shooter Tutorial player controls to touch. If anyone could help me I would really appreciate it. I have referred to the unity references, but i don't understand how to put it all together. Thank you and hope to hear from you.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Guntereno · Apr 05, 2014 at 02:40 PM

Hi there,

I've just been through the Space Shooter sample myself and wanted to do the same thing. This is what I ended up with as my FixedUpdate() function. I wanted it to work with mouse or touch so it worked in the game view too. I basically set a target position whenever the user touches, and set the velocity to move towards the target. I've also exposed a public dampingRadius so the ship can slow down as it reaches its target.

using UnityEngine; using System.Collections;

[System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; }

public class PlayerController : MonoBehaviour {

  public Boundary boundary;
  public float dampingRadius;
  public float fireDelay;
  public GameObject shot;
  public Transform shotSpawn;
  public float speed;
  public float tilt;
  public float velocityLag;
 
  private Vector3 target;
  private float nextFire;
 
 
  void Start () {
  }
 
  void Update() {
      bool triggered = false;
      if (Input.mousePresent && Input.GetMouseButton(0))
      {
          triggered = true;
      }
      else if (Input.touchCount > 0)
      {
          triggered = true;
      }
 
      if (triggered && (Time.time > nextFire))
      {
          nextFire = Time.time + fireDelay;
          Instantiate(shot, shotSpawn.position, Quaternion.identity);
          audio.Play();
      }
  }
 
  void FixedUpdate ()
  {
      Vector3? touchPos = null;
      if (Input.mousePresent && Input.GetMouseButton(0))
      {
          touchPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f);
      }
      else if(Input.touchCount > 0)
      {
          touchPos = new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 0.0f);
      }
      if (touchPos != null)
      { 
          target = Camera.main.ScreenToWorldPoint(touchPos.Value);
          target.y = rigidbody.position.y;
      }
 
      Vector3 offset = target - rigidbody.position;
      
      float magnitude = offset.magnitude;
      if (magnitude > dampingRadius)
          magnitude = dampingRadius;
      float dampening = magnitude / dampingRadius;
 
      Vector3 desiredVelocity = offset.normalized * speed * dampening;
 
      rigidbody.velocity += (desiredVelocity - rigidbody.velocity) * velocityLag;
 
      // Clamp to bounds
      rigidbody.position = new Vector3
      (
          Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
          0.0f,
          Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax)
      );
 
      rigidbody.rotation = Quaternion.Euler(0.0f, 0.0f, rigidbody.velocity.x * -tilt);
  }

}

Comment
Add comment · Show 8 · 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 RabbiTo4ek · Oct 01, 2014 at 05:44 PM 0
Share

Hi,

I tried this code, but I'm not sure what the target is set too in the first place? Is it a global variable, if so which value is used? Similar with:dampingRadius & velocityLag.

do you have the full code for this?

Regards

avatar image Guntereno · Oct 03, 2014 at 01:08 PM 0
Share

Hi there,

Sorry, I didn't paste the whole code. I'll post it when I get home tonight.

"target" is a member variable of the $$anonymous$$onoBehaviour. As it's only updated when the player touches (or presses the mouse button) the ship will continue moving towards the stored target position.

avatar image RabbiTo4ek · Oct 10, 2014 at 11:25 AM 0
Share

Hi Guntereno,

Any update on this?

It would really help me if you can provide that full code or even project in a zip.

It would be great if you can attach to this ticket or my email: e.lukiyanov@edu.salford.ac.uk

Regards Evgeny

avatar image Goremaster · Oct 13, 2014 at 08:53 PM 0
Share

Hi,

Also keen to see the full code for this touch movement! Would love for anyone to also share it if they get anything!

avatar image Athanateus · Oct 20, 2014 at 01:50 PM 0
Share

Perfect - took me a few moments to realise I needed to declare target as private and dampeningRadius and velocityLag as public variables but works perfectly. I have a touch screen monitor and saw both touch and key controls working!

Thanks

Show more comments
avatar image
0

Answer by glenpyeldho · Jul 22, 2015 at 01:23 PM

https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/space-shooter-to-mobile

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

24 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

Related Questions

One more question about touches 1 Answer

Swipe Control Issue.. Need help 1 Answer

Separate functions for separate touches 1 Answer

This code only works on 3D Colliders and I want it to work on 2D. What do I need to change? 1 Answer

Use touch to spawn a prefab 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