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 alexd991 · Mar 16 at 08:38 AM · transformcharactercontrollertransform.position

Changing transform.position does not update the objects position

I'm creating a drone for my player that will launch from their back.

When activating the drone, the player's camera is disabled and the drone's camera is enabled.
The drone's camera clips into the players back which is unacceptable. To stop this happening I created a method that will 'launch' the drone from above their head, with a Raycast checking above them incase of an obstacle.

Here is the launch code from the Drone script:

 void launch(){
         Vector3 launchPos;
         RaycastHit hit;
 
         if(Physics.Raycast(transform.position, Vector3.up, out hit, maxLaunchHeight)){
             launchPos = hit.point;
             launchPos.y -= 0.25f;
         } else {
             launchPos = transform.position;
             launchPos.y += maxLaunchHeight;
         }
         Debug.Log("before: "+transform.position);
         transform.position = launchPos; 
         Debug.Log("after: "+ transform.position); 
     }


The problem I am having is that updating transform.position does not actually change the position of the object; it is not raised above their head upon activation.

I can see through debugging that transform.position.y is being changed to the desired value but it doesn't appear it is actually being applied to the object.

Strangely, I can change transform.position in the Update method - to help debug I added a bool that if true would set the position to some vector and when setting it to true mid-game it moves the drone to that vector without issue.

Any ideas as to why this might be happening?
If there is not enough info please let me know and I'll be happy to post more code / try and explain it better.

Comment
Add comment · Show 3
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 Narc0t1CYM · Mar 16 at 03:25 PM 0
Share

It seems to me that this code is not wrong per se, I think transform.position gets overwritten in your Update() method. Check the flow of your Update() method

avatar image alexd991 Narc0t1CYM · Mar 17 at 11:46 AM 1
Share

Thank you for the reply. I took your advice and sequentially commented out the methods affecting transform.position until it became clear it was the CharacterController overwriting my changes.

avatar image Narc0t1CYM alexd991 · Mar 17 at 11:52 AM 0
Share

No worries, glad you've found the solution! Going through your code from start to finish, line by line is called dry-running. Often it's the only way to catch an error :)

1 Reply

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

Answer by alexd991 · Mar 17 at 11:45 AM

I have solved the problem.
I am using a CharacterController to move the drone around and it appears that CC overwrites transform.position. This means it was set for one frame only when launching and then CC took it back to its previous position.

To solve this I have disabled CC until after I have set transform.position and it now works as expected. Hope this helps anyone with a similar issue.

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

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

ping pong on x axis, unable to touch end of plane, 1 Answer

Need help updating camera position to go above the player 2 Answers

AI movement using Character Controller, turning problem 1 Answer

How do I get the transforms of all the objects in my scene ? 2 Answers

Check if there is a object in front of you and equal your velocity to that objects 0 Answers


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