- Home /
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;
}
}
}
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.
@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.
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)
Your answer
Follow this Question
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