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 PedroGarrano · Sep 01, 2015 at 01:32 PM · gameobjecttransform.position

After using transform.position, gameobject resets z-axis position

Hi everyone.

I'm trying to do something "simple": to make the gameobject move towards the player's touch (on the X-Y plane), while moving the gameobject along the Z-axis at a constant speed. The first part, I've managed. The second, not so much.

 using UnityEngine;
 
 public class Player : MonoBehaviour
 {
     //flag to check if the user has tapped
     //Set to true on tap. Reset to false on reaching destination
     private bool shouldmove = true;
     //destination point
     private Vector3 endPoint;
     private float zPosition;
 
     Ray ray;
     RaycastHit raycast;
 
     public float duration = 50f;
     public float FwSpeed = 1f;
 
     private Rigidbody _rigidbody;
 
     void Start()
     {
         zPosition = _rigidbody.position.z;
     }
     
     void Awake()
     {
         _rigidbody = GetComponent<Rigidbody>();
     }
 
     void Update()
     {
         if(Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(0).phase == TouchPhase.Moved))
         {
             ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
 
             if(Physics.Raycast(ray,out raycast))
             {
                 endPoint = raycast.point;
                 endPoint.z = zPosition;
                 Debug.Log(endPoint);
                 shouldmove = true;
             }
         }
 
 
         if(shouldmove && !Mathf.Approximately(_rigidbody.position.magnitude, endPoint.magnitude))
         {
             _rigidbody.transform.position = Vector2.Lerp
                 (
                 _rigidbody.position,
                 endPoint ,
                 1/(duration*(Vector2.Distance(_rigidbody.position, endPoint)))
                 );
         }
 
         else if(shouldmove && Mathf.Approximately(_rigidbody.position.magnitude, endPoint.magnitude))
         {
             shouldmove = false;
             Debug.Log("I am here");
         }
         _rigidbody.velocity = new Vector3(_rigidbody.velocity.x, _rigidbody.velocity.y, FwSpeed);
         zPosition = _rigidbody.position.z;
     }
 }

For some reason, it resets the gameobject.position.z to 0 after the transform.position. Any clue to as to why and how to fix it?

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

Answer by PedroGarrano · Sep 03, 2015 at 05:22 AM

Figured it out, re-wrote the code so it doesn't use transform.position, shifted all movement to rigidbody.velocity.

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

2 People are following this question.

avatar image avatar image

Related Questions

How to set a gameobjects transform to an empty child of another gameobject,set a gameobject transform to another gameobjects child transform 0 Answers

Operator `+' cannot be applied to operands of type `UnityEngine.Vector3' and `float' 3 Answers

make a gameobject move to another gameobject that was selected by mouse click? 1 Answer

Need Help (Script): UI reticle to RayCast gameObject > Transform gameObject to "hand" attached to FPS. 1 Answer

Find a specific object using transform 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