- Home /
Air Hockey Game mouse drag or follow
Hi, I´m basically new in unity and I´m working in a Air Hockey Game. I just Started to play with the Bouncy Material, The objects´ masses and stuff. But now I want the Paddle to move following the mouse moves or at least that I can drag it... and it´s resulting kind of imposibble to me... Please any help on this code??
You may want to describe HOW you want the user to interact with your game.
Do you want the mouse cursor to appear on screen, and you click-and-drag the puck? Or would you prefer the mouse cursor to be invisible and for the mouse movements to translate to puck movements straight away?
What language are you writing your scripts in?
@IgnorancelsBliss I just want the paddle to follow the mouse movements so the user moves the mouse in order to hit the puck... I use JavaScript
Answer by Atrius · Apr 16, 2012 at 04:13 AM
I haven't experimented with doing something like this with a RigidBody. So that may change the solution entirely. RigidBody would require the use of adjusting your Drag and using AddForce to push the paddle in the direction of the mouse cursor.
Working this like a Pong controller you can do the following:
1) Translate mouse cursor for the camera into a ray using Camera.ScreenPointToRay() 2) Perform a Raycast to hit your playing surface, giving you a point your players want the paddle to move to 3) In the Update move the paddle toward the target point
Here's an example (may not be the best, but it's an idea):
using UnityEngine;
using System.Collections;
public class PaddleController : MonoBehaviour {
private LayerMask tableLayer;
private Vector3 targetPoint;
public float inputPadding = 0.0f;
public float inputSpeed = 2.0f;
// Use this for initialization
void Start () {
tableLayer = 1 << LayerMask.NameToLayer("Table");
}
// Update is called once per frame
void FixedUpdate () {
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(mouseRay, out hit, tableLayer)) {
targetPoint = hit.point;
}
}
void Update() {
if (Vector3.Distance(transform.position, targetPoint) > inputPadding) {
Vector3 pos = Vector3.Lerp(transform.position, targetPoint, Time.deltaTime * inputSpeed);
transform.position = new Vector3(pos.x, transform.position.y, pos.z);
}
}
}
Thank you very much... your code really helps a lot... I got it almost all of it (maybe because I use JavaScript)... except for:
void Start () {
tableLayer = 1 << Layer$$anonymous$$ask.NameToLayer("Table");
}
rightnow I´m working with rigidbodies, so if have some tips or code with rigidbodies it will be well received
Remember - don't put physics on any object in Unity unless you want it to interact properly with the world around it.
So a rigidbody is an excellent idea for the various parts, but do you want the actual PADDLE to have mass an intertia? If you do this, it would mean that the user will have to fight the puck when they want to change direction. This means the control will be 'sloppy' and less accurate, but it could be a little more realistic.
If you don't want it to have real mass and inertia, just use transform.position like the example above.
If you want it to have mass and you want the player to fight harder to change the paddle direction, add a CharacterControll and Character$$anonymous$$otor to the object, and change it's direction using the 'input$$anonymous$$oveDirection' member of the motor.
Check the FPS Controller script to see it in action.
input$$anonymous$$oveDirection is a Vector - it tells the motor that you want the puck to head in the direction of the vector.
So rather than the puck 'following' the cursor, the puck will respond directly to your mouse movements - moving the mouse left will move the puck left, regardless of the position of the cursor on the screen.
What I am doing in the on start is merely assigning the Layer of the table surface to a variable. Physics.Raycast() allows you to pass a Layer$$anonymous$$ask which is the layer that the object is assigned to in the Inspector. This will make the ray cast out from the camera only hit the table, and not the paddle or other objects in the 3d scene. If you go to your table surface and assign it to a layer you can just hardcode the number in the Raycast(). See: http://unity3d.com/support/documentation/ScriptReference/Layer$$anonymous$$ask.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613070359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making an object replace the cursor 0 Answers
Mouse follow on network 0 Answers
RaceGame - Car Follow Mouse 1 Answer