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 Daniel Greenhorn · Nov 08, 2014 at 06:18 AM · vector3transform.positionmovesubtract

Vector3 math problem?

Got unpredictable results with the following script in C#

 void FixedUpdate ()
 {
 //move to position endpoint
 if (Input.GetKey ("z")) {
     var pos = transform.position;
     var endpoint = new Vector3 (5, 5, 5);
     transform.position = endpoint - pos; }
 //come back from position
 if (Input.GetKey ("x")) {
     var pos = transform.position;
     var startpoint = new Vector3 (0, 0, 0);
     transform.position = startpoint - pos; }

}

The code should move the object from whatever position it may be to endpoint when "z" is pressed (and stay there if z is pressed again) and move to startpoint if "x" is pressed (and stay there).

Problem_1:object at (0,0,0), keep pressed z, : object flickers very fast between origin and designated endpoint. Why is that? I would understand to flicker in place as FixedUpdate means once per frame the position is verified and if already at 5,5,5 the difference vector should be zero (no change in position).

Problem_2:object at (0,0,0), alternate pressing z and x: object gets to progresively further positions. Why? The vector difference should bring the object from where ever it may be to the designated positions.

Problem_3: continue some time alternating z and x and the object should get to positions theoretically unobtainable from the fixed coordinates. For example, how does it get to a transform x axis of 6 adding and subtracting zeros and fives? I would understand if it were a smooth movement, but in this case it's move from A to B in one frame.

Problem_4:sometimes, with slightly larger code, pressing the keys does not produce any result (about one in 4 keystrokes gets the result). Why? It's as if the action must accur between a specific interval inside each frame or be ignored. Long and hard keystrokes seem to improve the rate. Is there a "pressed" value to diminish to make the scrip more responsive?

That's about it. Thanks. PS. Hope this get's posted

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by zharik86 · Nov 08, 2014 at 07:43 AM

You incorrectly work with vectors. The transform.position method returns an object position vector in world coordinates. If you only subtract a vector, anything at you it not to turn out. Let's look in numbers on your example with pressing of "x". The function FixedUpdate() is carried out each 0.1 second by default. For example, initially, your object had coordinates (5, 4, 3). Then at the first execution of a code: (0, 0, 0) - (5, 4, 3) = (-5,-4,-3). Further, second execution of a code: (0, 0, 0) - (-5,-4,-3) = (5, 4, 3). And, etc. Here from where blinking. I will write a code which will smoothly move object. And it is better to move it to the function Update() (write on CSharp):

  public float mySpeed = 5.0f; //speed for smooth moving, if very fast, change value in Inspector

  void Update () {
   //move to position endpoint
   //Better use KeyCode
   if (Input.GetKey(KeyCode.Z)) {
    Vector3 endpoint = new Vector3 (5, 5, 5);
    //Smooth move, using Lerp function
    transform.position = Vector3.Lerp(transform.position, endpoint, mySpeed * Time.deltaTime);
   }
   //come back from position
   if (Input.GetKey(KeyCode.X)) {
    Vector3 startpoint = new Vector3 (0, 0, 0);
    //Smooth move, using Lerp function
    transform.position = Vector3.Lerp(transform.position, startpoint, mySpeed * Time.deltaTime);
   }
  }

I hope that it will help you.

Comment
Add comment · Show 2 · 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 Daniel Greenhorn · Nov 09, 2014 at 06:15 AM 0
Share

Thank you very much, zharik86 and tanoshimi. I can't believe I didn't try the simplest way first. Got into thinking vector3 was a force vector, hence the difference B-A to get a new force to get from A to B.

Case is utterly solved.

avatar image zharik86 · Nov 09, 2014 at 07:34 AM 0
Share

@$$anonymous$$-Greenhorn If I or everybody help you, please, mark my/him answer (below vote button).

avatar image
1

Answer by tanoshimi · Nov 08, 2014 at 07:57 AM

If you just want to "teleport" the object straight to the two positions, you don't need to calculate the difference from the current location - just assign the destination position directly:

 void Update ()
 {
     //move to position endpoint
     if (Input.GetKey ("z")) {
       transform.position = new Vector3 (5, 5, 5);
     }
     //come back from position
    if (Input.GetKey ("x")) {
         transform.position = new Vector3 (0, 0, 0);
    }
 }

Problems 1,2,3 are because you are assigning a vector difference, rather than either assigning the destination or adding the difference. Problem 4 is because you are using FixedUpdate instead of Update.

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

My gameobject moves up wrong 1 Answer

How to get a proper ramming effect? 1 Answer

Trouble converting transform.position to C# 1 Answer

How would I move an Object after collision? 1 Answer

How do I change one value of a vector? 2 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