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 /
  • Help Room /
avatar image
0
Question by Superrodan · Nov 11, 2015 at 10:02 PM · physicsrigidbodymoveposition

Moving a rigidbody in local space with transform.TransformPoint has unexpected results.

Hey all,

This is the first time I've ever used transform.TransformPoint and I am not sure if I am using it incorrectly, but it's definitely not working as I intended.

This script should be extremely simple. All it does is take a gameObject that I have assigned in the editor, get the rigidbody component to it (which is a Kinematic rigidbody so the elevator doesn't fall), then if the elevator bool is set to rise in another script, it moves the elevator up.

I am attempting to accomplish this by calculating the position I want to move the elevator's gameObject to in local space and then using the transform.TransformPoint function to convert that to world space. Once I do, I am using MovePosition.

instead of working, when I activate the elevator, it goes crazy and flies off in a random direction before giving me a ton of errors.

NOTES: I NEED the elevator to move in its local y because I use "elevators" for other things besides rising up, such as to push blocks sideways. This is why I am not just using the world space position of the rigidbody when determing where to move it.

Here is my code.

     public float riseSpeed;
     public float riseDistance;
     public bool rise;
 
     public GameObject elevatorObject;
     Rigidbody elevator;
     private Vector3 startPosition;
 
     //Elevators rise and take things on top of them with them.
     void Start () {
         startPosition = elevatorObject.transform.localPosition;
         elevator = elevatorObject.GetComponent<Rigidbody>();
     }
 
     void Update()
     {
         //Elevator should be rising.
         if(rise==true)
         {
             //Find elevator's current position.
             Vector3 currentPos = elevatorObject.transform.localPosition;
             //Add to the y
             currentPos.y+=riseSpeed*Time.deltaTime;
             //Make sure we haven't gone past the height limit.
             if(currentPos.y >= (startPosition.y+riseDistance))
             {
                 currentPos.y = startPosition.y+riseDistance;
             }
             //THIS IS THE STEP I AM CONFUSD BY. Because I want to move a rigidbody instead of the transform I need to convert to worldspace
             currentPos = transform.TransformPoint(currentPos);
             //Now move the elevator.
             elevator.MovePosition(currentPos);
         }
     }

I am sure I'm doing something I'm not supposed to be here somewhere, I just don't know what it is.

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

1 Reply

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

Answer by OncaLupe · Nov 12, 2015 at 04:11 AM

transform.localPosition, when used on an object without a parent, just returns the world position. It's meant to move child objects relative to its parent without needing to convert between world coordinates. TransformPoint() may work in that situation, however to just move an object along an axis is actually very easy. Each object's transform component has variables that point along each of it's local axis. transform.up points in world space where it's local Y axis is. Multiply the distance you want to travel to that and add to the start position.

 using UnityEngine;
 using System.Collections;
 
 public class MoveInLocalY : MonoBehaviour
 {
     public float riseSpeed;
     public float riseDistance;
     public bool rise;
 
     public GameObject elevatorObject;
     Rigidbody elevator;
     Vector3 startPosition;
 
     float currentRiseOffset = 0f;
 
     void Start()
     {
         startPosition = elevatorObject.transform.position;
         elevator = elevatorObject.GetComponent<Rigidbody>();
     }
     
     void Update()
     {
         if(rise)
         {
             //Add intended movement to current offset
             currentRiseOffset += riseSpeed * Time.deltaTime;
             //If we reached (or passed) the end position, set exact value
             if(currentRiseOffset >= riseDistance)
             {
                 currentRiseOffset = riseDistance;
                 //Turn off the bool, no need to keep trying to move once we reach the end
                 rise = false;
             }
             //Move elevator. Takes the local Y axis, multiples by currentRisePos to get total offset, then adds to startPosition.
             elevator.MovePosition(startPosition + (elevatorObject.transform.up * currentRiseOffset));
         }
     }
 }

You may notice the if() check changed slightly also. if(rise==true) is redundant since 'rise' is a bool already. If you need to check if a bool variable is false, use if(!rise). ! means 'not'.

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 Superrodan · Nov 12, 2015 at 06:08 AM 1
Share

So I actually solved this a different way, by changing one line of code from:

      currentPos = transform.TransformPoint(currentPos);

to

      currentPos = transform.parent.TransformPoint(currentPos);

I asked a friend for help and that's what we came up with. It ended up working perfectly. I didn't get a chance to post it as a solution here until just now.

The reason why the above change works is still very confusing to me, it has something to do with trying to use its own reference point when its position is reliant on its parent as a reference point. Still, it works now and I figured I'd put that here in case someone else has the exact same issue.

Your solution does look like it would work as well, based on the information I provided, I haven't tested it but I'll accept it as an answer since you took the effort to respond and I don't see any problems with it at first glance.

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

38 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 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 Properly Move Two Attached Kinematic Rigidbodies 1 Answer

Moving RigidBody Smoothly 0 Answers

Moving a cube with RigidBody. MovePostition, it randomly stops moving 0 Answers

AddForce and Momentum‏ 1 Answer

How to create chain with realistic physics 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