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 Catlinman · Oct 02, 2012 at 01:58 PM · rigidbodytransformvector3constraint

Object only moves on global x axis

Hey guys, I am having a problem in my script where I want let the player open a drawer by using the mouse. The drawer then moves outward depending on the speed of the mouse Y movement. Everything works fine when the drawer object is aligned to the global x axis. My problem is that I don't want it to move on the global x axis but instead on its own local x axis.

As you can see the drawers local x axis is pointing away from the cube - The drawer on the left has the x axis pointing out the same way but the only one that works is the one in the middle which is aligned with the global x axis. The other one doesn't even move at all. They are both exactly the same but have different rotations.

alt text

The code:

 #pragma strict
 
 var startDistance :float;
 var endDistance : float;
 var axisInverted : boolean;
 private var startPoint : Vector3;
 private var endPoint : Vector3;
 private var endPosition : Vector3;
 private var startPosition : Vector3;
 private var lockedPosition : Vector3;
 
 function Start(){
     lockedPosition = Vector3(transform.localPosition.x,transform.localPosition.y,transform.localPosition.z);
     
     if(!axisInverted){
         startPoint = transform.localPosition - transform.InverseTransformDirection(Vector3.right)*(startDistance-.01);
         startPosition.x = startPoint.x;
         endPoint = transform.localPosition + transform.InverseTransformDirection(Vector3.right)*endDistance;
         endPosition.x = endPoint.x;
     }
     else{
         startPoint = transform.localPosition - transform.InverseTransformDirection(Vector3.left)*(startDistance-.01);
         startPosition.x = startPoint.x;
         endPoint = transform.localPosition + transform.InverseTransformDirection(Vector3.left)*endDistance;
         endPosition.x = endPoint.x;
     }
 }
 
 function Update () {
     transform.localPosition = Vector3(transform.localPosition.x,lockedPosition.y,lockedPosition.z);
     var moveSpeed = Input.GetAxis("Mouse Y");
     if(!axisInverted){
         if(Input.GetButton("Use")){
             rigidbody.AddRelativeForce(-Vector3.right*moveSpeed,ForceMode.Impulse);
         }    
         if(transform.localPosition.x >= startPoint.x){
             rigidbody.velocity = Vector3.zero;
             transform.localPosition.x = transform.localPosition.x - 0.01;
         }
         
         if(transform.localPosition.x <= endPoint.x){
             rigidbody.velocity = Vector3.zero;
             transform.localPosition.x = transform.localPosition.x + 0.01;
         }
     }
     else{
         if(Input.GetButton("Use")){
             rigidbody.AddRelativeForce(-Vector3.left*moveSpeed,ForceMode.Impulse);
         }    
         if(transform.localPosition.x <= startPoint.x){
             rigidbody.velocity = Vector3.zero;
             transform.localPosition.x = transform.localPosition.x + 0.01;
         }
         
         if(transform.localPosition.x >= endPoint.x){
             rigidbody.velocity = Vector3.zero;
             transform.localPosition.x = transform.localPosition.x - 0.01;
         }
     }
 }

I hope you understand what I am trying to do and it would be great if you could check the code. It should all be set for local coordinates but I may have missed something.

unity_answer_drawer.png (220.0 kB)
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 Catlinman · Oct 05, 2012 at 10:21 PM 0
Share

bump for feedback

avatar image Catlinman · Oct 07, 2012 at 06:33 PM 0
Share

So I found that the cause of the problem is 'lockedPosition = Vector3(transform.localPosition.x,transform.localPosition.y,transform.localPosition.z);' and 'transform.localPosition = Vector3(transform.localPosition.x,lockedPosition.y,lockedPosition.z);'. Does anyone know how to fix this so it only gets the local coordinates?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Oct 05, 2012 at 10:23 PM

This line looks suspect:

if(transform.position.x >= endPoint.x){

Comment
Add comment · Show 1 · 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 Catlinman · Oct 06, 2012 at 08:54 AM 0
Share

I changed it to localPosition but it wasn't the cause of the problem.

avatar image
0

Answer by MikeLouns · Oct 09, 2012 at 12:50 AM

transform.forward will allow you to move objects according to their local axis

Comment
Add comment · Show 1 · 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 Catlinman · Oct 09, 2012 at 11:35 AM 0
Share

yes i do know that but I need it to lock the starting y,z position so it doesn't move off the x axis when colliding with the player for example

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

11 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

Related Questions

Guided missile help c# 1 Answer

Npc Movement 0 Answers

Animation or smooth teleportation 0 Answers

Rigidbody to follow player around 1 Answer

Moving a Platform 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