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 Hydroid · Dec 13, 2016 at 02:57 AM · inputinput.getaxisinput.touchgetmousebutton

How to get an object to follow touch when held. (move on y axis)

Hello! I'm trying to make a pong-like game and I need the player to be able to move the block to deflect the ball. I'm making this for iOS and I would like to be able to move it using just tapping and holding to move on the Y-axis. I've looked online and could not figure out what to use. I found something that works on PC, but not Mobile. Here is the script:

 public class Paddle : MonoBehaviour 
 {
 public float paddleSpeed = 1;
 public Vector3 playerPos;
 
 void Update()
 {
 float yPos = transform.position.y + (Input.GetAxis("Vertical") * paddleSpeed);
 playerPos = new Vector3(-36, Mathf.Clamp(yPos, -11.58f, 11.58f), 0);
 transform.position = playerPos;
 }
 }

What I think needs to be changed is the (Input.GetAxis("Vertical") * paddleSpeed). but I can't get it to work with GetMouseButton(0). P.S. I'm new here so I don't know if I have this in the right place. Anyways, thanks in advance!

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 BlastOffProductions · Dec 13, 2016 at 06:07 AM

Now I know this works.

 using UnityEngine;
 using System.Collections;
 
 public class ClickDrag : MonoBehaviour {     public GameObject character;
     public float speed = 50;
 
     void Update () 
     {
 
         if(Input.touchCount == 0)
         {
             Vector3 target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.z,Input.mousePosition.y, 50.0f));
             character.transform.Translate(Vector3.MoveTowards(character.transform.position, target, speed * Time.deltaTime) - character.transform.position);
 
         }
 
     }
 }

Comment
Add comment · Show 18 · 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 Hydroid · Dec 13, 2016 at 09:13 PM 0
Share

I get no errors but it doesn't move when I click and hold the paddle.

avatar image BlastOffProductions Hydroid · Dec 13, 2016 at 10:14 PM 0
Share

Sorry, didn't give instructions. First put this script on your camera, then drag the paddle game object to the available public space on the script.

avatar image BlastOffProductions BlastOffProductions · Dec 14, 2016 at 05:50 PM 0
Share

Here's an example.

via GIPHY

Show more comments
Show more comments
avatar image BlastOffProductions Hydroid · Dec 17, 2016 at 05:56 PM 0
Share

Hmm, it could be when you pasted the code, you accidentally didn't delete all the old code first. This has happend to me a few times. Also, I noticed (from the video) that on the paddles rigidbody; the rotations are all frozen, and the Z position is frozen too. This might have something to do with it. Also the might what to check if you have multiple cameras in your scene. Because the script targets: (Camera.main.ScreenToWorldPoint), so multiple cameras active in the scene, might mess with the code.

Hope this helps, if it doesn't i'm still here.

avatar image Hydroid BlastOffProductions · Dec 17, 2016 at 07:00 PM 0
Share

I checked everything and nothing has changed. This is weird why this is happening. Is there anything you want me to show you?

Show more comments
avatar image BlastOffProductions Hydroid · Dec 17, 2016 at 09:08 PM 0
Share

Yes, I was wondering what is attached to the paddle before you press play.

avatar image Hydroid BlastOffProductions · Dec 17, 2016 at 09:54 PM 0
Share

alt text

That's what is attached to the paddle. I also tried moving the burger away and it continued to circle around and get bigger.

12.png (94.3 kB)
Show more comments
avatar image
0

Answer by Laiken · Dec 18, 2016 at 12:45 AM

(I'm not sure this works on iOS, I only use windows)

Perhaps what you are looking for is OnMouseDrag()? https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDrag.html

You can make a flat collider (similar to a canvas, but instead of a canvas, it's a collider) on the whole screen and put it on a Layer called CanvasCollider, then when clicking and dragging with the mouse (not sure if it's the same for touch) the object will slide on the collider

(you need to put this on the object that will be moved)

 void Start ()
     {
         canvasColliderMask = LayerMask.GetMask("CanvasCollider");
         cam = Camera.main;
 
     }
 
 void OnMouseDrag()
     {
         RaycastHit hit;
         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit, 100, canvasColliderMask))
         {
             transform.position = new Vector3(transform.position.x, hit.point.y, transform.position.z);
         }
 
         
     }
Comment
Add comment · Show 4 · 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 Hydroid · Dec 18, 2016 at 03:35 AM 0
Share

It gives me 21 errors after doing what you asked lol

avatar image Laiken Hydroid · Dec 18, 2016 at 05:22 AM 0
Share

lol!

I don't know why you got those errors since I copied that from my own game and it gives me no errors. I guess I should not try to answer stuff related to another plataform. Sorry about that. :|

avatar image Laiken Hydroid · Dec 18, 2016 at 05:45 AM 2
Share

I will give you one last suggestion that will not take much time for you to test because it adds very little to your own code (just copy and paste it on your script):

  public class Paddle : $$anonymous$$onoBehaviour 
  {
  public float paddleSpeed = 1;
  public Vector3 playerPos;
 
 float yPosOnLastFrame;
  
  void Update()
  {
 float yPosOffset = Input.mousePosition.y - yPosOnLastFrame;
 yPosOnLastFrame = Input.mousePosition.y;
 
  float yPos = transform.position.y + yPosOffset  * paddleSpeed);
  playerPos = new Vector3(-36, $$anonymous$$athf.Clamp(yPos, -11.58f, 11.58f), 0);
  transform.position = playerPos;
  }
  }

if the movement is inverted, multiply yPosOffset by -1

avatar image Hydroid Laiken · Dec 18, 2016 at 03:48 PM 1
Share

It works! Although it's a little buggy, I'm sure I can find a way to make it on point with the touch. Anyways, thank you! (It doesn't let me upvote since I don't have 100 rep but if you reply to the main post, I'd be happy to make it the answer)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

PlayStation Vita input 1 Answer

Input Snap option not returning 0? 0 Answers

input resets after scene change,Held Down Input not responding after scene change. 0 Answers

How to convert Input.GetAxis to Accelerometer control? 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