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 Drksteel · Sep 17, 2015 at 07:29 PM · vector3axismouseclickmouse-drag

I need help with moving an object on only the Y axis with mouse input

I need help badly and i don't know where i'm going wrong, i'm trying to move a ball on the Y axis only when i click and drag the mouse. When i click, it's not following the ball, and when i go too far, the ball just goes crazy and i can't figure out why its doing this. This is the part of the code that i need help with.

 if (Input.GetMouseButton(0)){
             float TouchPosY = (float) ((Input.mousePosition.y/1000)* 10);
             Vector3 TouchPos = new Vector3 (0.0f, TouchPosY, 0.0f);
             if(Input.mousePosition.y/185 - 1.1 > 0){
          // This is the line where it goes up
                 transform.position = Vector3.MoveTowards(transform.position, TouchPos, 10 * Time.deltaTime);
             } else if (Input.mousePosition.y/185 - 1.1 < 0) {
    // This is the line where it goes down
 transform.position = Vector3.MoveTowards(transform.position, TouchPos, -10 * Time.deltaTime);
             }
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 Cherno · Sep 17, 2015 at 07:42 PM 1
Share
  1. Store x and z position in temp variables.

  2. Apply transformation.

  3. Reassign position using the stored x and z values and the current y value.

     float pos_x = transform.position.x;
        float pos_z = transform.position.z;
        //transform.position = ..;
        transform.position = new Vector3(pos_x , transform.position.y, pos_z );
    
    

Or do it the other way around:

   Vector3 pos = transform.position;
    //pos.y = ...
    transform.position = pos;



avatar image holliebuckets ♦♦ · Sep 17, 2015 at 08:55 PM 0
Share

Would constraints be an option for you??

http://docs.unity3d.com/ScriptReference/RigidbodyConstraints.html

You could use RigidbodyConstraints.None when the mouse button is up to remove the X and Z constraints. :) Just another option

1 Reply

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

Answer by maccabbe · Sep 18, 2015 at 03:06 AM

First don't directly use the position of the mouse on the screen to determine the position of an object in the world, Screen Space != World Space.

Second, if your target is below your current position then you still want to go towards your target. To go towards a target use a positive value for the third variable of Vector3.MoveTowards. It doesn't matter if you are going up or down but if you are going towards or away to the target.

If you are using an orthographic camera then you can convert screen coordinates to world coordinates with Camera.ScreenToWorldPoint. For instance

 using UnityEngine;
 
 public class NewBehaviourScript : MonoBehaviour{
     void Update () {
         if (Input.GetMouseButton(0))
         {
             float TouchPosY = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;
             Vector3 target = new Vector3(0.0f, TouchPosY, 0.0f);
             transform.position = Vector3.MoveTowards(transform.position, target, 10 * Time.deltaTime);
         }
     }
 }

If you are using a perspective camera then you need more details on how you want to calculate the target y value. For instance, you could use Camera.ScreenPointToRay to get the ray coming out of the mouse and calculate the point on the line x=0 closest this ray then use the position of this point as the target position.

http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

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

Get angle around a specified axis. 1 Answer

Distance interaction 0 Answers

Drag GameObject with mouse in just one axis 2 Answers

Problem with lerping Y axis 0 Answers

Make the gameobject flip to the direction it is being dragged to? 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