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 kildare268 · Dec 29, 2014 at 01:31 AM · raycastraycasthittouch controlspong

Using Touch to Move Pong Paddles

Hi,

I need some help. I have 2 game objects - player1 and player2. They are paddles in a Pong game.

When I add the JS script below as a new component to the player1 and player2 game objects and then build and run it, both paddles move together when I touch and drag left or right just one of the paddles.

How can I modify the script so that the paddles move separately e.g. I touch player1, drag it to the left or right then lift my finger and touch player2 and drag it left or right without player1 moving or only move when I lift my finger and touch player1 again?

Simon

JS Script

private var ray : Ray;

private var rayCastHit : RaycastHit;

function Update( )

{

 if (Input.GetMouseButton(0))

 {

     ray = Camera.main.ScreenPointToRay (Input.mousePosition);

     if (Physics.Raycast (ray, rayCastHit))

     {

         transform.position.x = rayCastHit.point.x;

     }

 }

}

screen shot 2014-12-28 at 9.50.26 pm.png (7.4 kB)
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 yashpal · Dec 29, 2014 at 05:27 AM

hello @kildare268, I am not very familiar with javascript. so i can only able to give you how to solve problem. you can use OnMouseDown function. this is only called when you click on that mouse and and use OnMouseDrag

I make this C# script for me to drag and move object.

     public float xPositionOfMouseOnScreen;
     public float yPositionOfMouseOnScreen ;
     public float xPositionOfCamera;
     public float yPositionOfCamera;
     
     Vector2 OffsetLocation ;
 
     // Use this for initialization
     void Start ()
     {
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         
     }
     
     void OnMouseDown()
     {
         Vector2 mousePositionTemp;
         
         mousePositionTemp = Input.mousePosition;
         xPositionOfMouseOnScreen = mousePositionTemp.x;
         yPositionOfMouseOnScreen = mousePositionTemp.y;
         
         Vector3 positionTemp;
 
 
 
         positionTemp = Camera.main.ScreenToWorldPoint (new Vector3 (mousePositionTemp.x, mousePositionTemp.y, 0));
 
 
         xPositionOfCamera = positionTemp.x;
         yPositionOfCamera = positionTemp.y;
         
         OffsetLocation = transform.position - positionTemp;
     }
     void OnMouseDrag () 
     {
         Vector2 mousePositionTemp;
         
         mousePositionTemp = Input.mousePosition;
         xPositionOfMouseOnScreen = mousePositionTemp.x;
         yPositionOfMouseOnScreen = mousePositionTemp.y;
         
         Vector3 position = Camera.mainCamera.ScreenToWorldPoint (new Vector3 (mousePositionTemp.x,mousePositionTemp.y,0));    
         
         xPositionOfCamera = position.x;
         yPositionOfCamera = position.y;
         
         
         transform.position = new Vector2(xPositionOfCamera + OffsetLocation.x, transform.position.y);
 
 //        print ("transform.position :" + transform.position + " OffsetLocation : " + OffsetLocation +" input.mousePosition : "+ Input.mousePosition +" position : " + position) ;
 
     }
     
     void OnMouseUp () 
     {
 
     }
 

if you have any question don't hesitate to ask.

Comment
Add comment · Show 2 · 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 yashpal · Dec 30, 2014 at 04:35 AM 0
Share

@Simon (kildare268),

just change this line

  transform.position = new Vector2(xPositionOfCamera + OffsetLocation.x, transform.position.y);

with

 transform.position = new Vector2($$anonymous$$athf.Clamp( xPositionOfCamera + OffsetLocation.x ,-5,5), transform.position.y);

I didnot tested this.

avatar image kildare268 · Jan 01, 2015 at 09:01 AM 0
Share

Hi @yashpal,

Thanks again!

Simon

avatar image
0

Answer by kildare268 · Dec 29, 2014 at 03:03 PM

Hi @yashpal,

Thanks so much! The script works!

Just 1 more question: How do I set the left and right drag limit? Based on the script you gave me, I need to set xPositionOfCamera + OffsetLocation.x not < -5 and not > 5. So how do I modify the code?

Simon (kildare268)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RaycastHit always returns 0 1 Answer

C# raycast shooting script broken 1 Answer

Mesh polygon of a hit surface on a gameobject 1 Answer

Raycast collision issue 0 Answers

How to place an object in the direction of a raycast 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