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 JVaughan · Apr 26, 2013 at 12:53 AM · c#transform.positionsetting

Problems setting transform.position.z in C# script.

I'm trying to set an empty GameObjects local Z to a distance set by a RayCast Collision. I seem to have everything working... but the correct method to set the Z value. Would love to know the correct way to do this, apparently position.Set isn't getting me there.

 using UnityEngine;
 using System.Collections;
 
 public class focalPtColDist : MonoBehaviour     {
     
     public float distToCol = 0.0f;
     public Vector3 posTemp;
     public Vector3 hitPosTemp;
     
     RaycastHit focalPtHit = new RaycastHit();
     Ray focalPtRay = new Ray();
 
     void Update() {
         
         focalPtRay = Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0.0f));
         posTemp = transform.position;
 
        // if (Physics.Raycast(myray, out myhit, 1000.0f) && Input.GetMouseButtonDown(0))
         if (Physics.Raycast (focalPtRay, out focalPtHit, 500.0f)) {
             print ("We Hit!");
             hitPosTemp = focalPtHit.transform.position;
             distToCol = Vector3.Distance(hitPosTemp, posTemp);
             transform.position.Set(0.0f,0.0f,distToCol);
             
         }
     }
 }
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
2
Best Answer

Answer by cagezero · Apr 26, 2013 at 01:26 AM

 transform.position = new Vector3(0,0,distToCol);
Comment
Add comment · Show 3 · 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 whydoidoit · Apr 26, 2013 at 06:06 AM 2
Share

@JVaughan To clarify why you are having the problem with your existing method: Transform.position is a property and Vector3 is a struct - unfortunately this means that when you do a .Set() on it, you are setting it on a temporary copy returned by the property's get function which is then immediately discarded.

avatar image JVaughan · Apr 26, 2013 at 11:58 AM 0
Share

Thanks guys, this is obviously an important concept and I was having a bit of difficulty groking it. I have the script behaving as I was expecting now.

avatar image cagezero · Apr 29, 2013 at 02:32 AM 0
Share

Glad to help! Since the answer solved your problem, please consider marking it as the correct answer!

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

14 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I go about moving my camera smoothly 1 Answer

GameObject1 move away when GameObject2 gets close 1 Answer

How do I make and object's position and rotation independent again? 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