• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Alp-Giray-Savrum · Mar 10, 2015 at 08:09 PM · movementmouseparentaxis

I can'^t move object in seceted axis

Hi guys, I've written a code piece, It Works actually but it isn't Works as i wanted.

 ///////////////////////////////////////////////////////////////////////////////
 //This script is written by Alp Giray Savrum
 //    SmoothMoveToMousePoint.js ; 10 March 2015, Monday 21.29
 ///////////////////////////////////////////////////////////////////////////////
 
 //Smooth Multiplier which is like SmoothFollow.js
 var smoothMultiplier : float;
 //Hit distance determines, where will our ray is going to stop ("Z" axis for directly forward),
 var hitDistance : int;
 //Our clicked target position holder. Also works for observation purposes.
 var clickedTargetPosition:Vector3;
 //Our real target position holder. Works for observation purposes too.
 var targetPosition : Vector3;
 
 //Is object going to be move in "x" axis ?
 var xAxis = false ;
 //Or will it move in "y" axis ?
 var yAxis = false;
 
 //Update function.
 function Update () {
 
     //Check for is player clicking left mouse ? (Default Mouse0 is Left, also works for touch at mobile devices)
     /*    GetKey, instead of GetKeyDown works for continuous input.    */
     if(Input.GetKey(KeyCode.Mouse0))
     {
         //Create a ray variable from camera point to screen click point (universe).
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         //Get clicked position's universal positions.        
         clickedTargetPosition = ray.GetPoint(hitDistance);
         
             //If object will move in "x" axis...
             if(xAxis) {
                 //Change only "x" axis.
                 targetPosition.x = clickedTargetPosition.x;
             }    
             
             //If object will move in "y" axis...
             if(yAxis) {
                 //Change only "y" axis.
                 targetPosition.y = clickedTargetPosition.y;
             }
     }
         //Change position smoothly to targetPosition.
         transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smoothMultiplier);
 
 }

At this code, I've programmed object to move in x axis when i click. It runs nice but, when i start to move camera (Parent object, has it's own movement script) object stucks at it's default place. I don't know why.

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

0 Replies

· Add your reply
  • Sort: 

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to control an object with a mouse? 1 Answer

Huh click and go script working 50% 0 Answers

Help with 2D AI scripting 2 Answers

moving an object with mouse ( my script is not smooth enough ) 0 Answers

Controller Joystick Hold Delay Logic? 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