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 manuelsilverio01 · Apr 16, 2012 at 03:05 AM · mouse follow

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??

Comment
Add comment · Show 2
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 IgnoranceIsBliss · Apr 16, 2012 at 03:42 AM 0
Share

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?

avatar image manuelsilverio01 · Apr 16, 2012 at 06:14 PM 0
Share

@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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
         }
 
     }
 }
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 manuelsilverio01 · Apr 16, 2012 at 06:20 PM 0
Share

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

avatar image IgnoranceIsBliss · Apr 16, 2012 at 10:40 PM 0
Share

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.

avatar image Atrius · Apr 16, 2012 at 11:17 PM 0
Share

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

avatar image manuelsilverio01 · Apr 17, 2012 at 07:02 PM 0
Share

Ok... Got it guys.. thanks a lot!!!

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

Making an object replace the cursor 0 Answers

Mouse follow on network 0 Answers

RaceGame - Car Follow Mouse 1 Answer

Need help with LookAt - Object flipping 1 Answer

How to make a cube follow my mouse pointer? 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