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 /
avatar image
0
Question by martygrof3708 · May 31, 2019 at 10:54 AM · rotationscripting problemtransformvector3vector2

Why does this code not work?

Essentially I am try to make a ball "jump", but without using physics because the ball has to travel a certain distance each time it jumps. Move() is called in update. JumpPosition is just the current position of the ball on a plane. EndJumpPosition is given to this script by another script.

 private void Move()
 {
     if (isJump)
     {
         jumpPosition = new Vector2(transform.position.x , transform.position.z);

         if (Mathf.Abs(jumpPosition.magnitude) < Mathf.Abs(endJumpPosition.magnitude) / 2)
         {
             Debug.Log("HalwayThere");
             transform.Translate(new Vector3(0 , 0.05f , 0));
         }
         else
         {
             transform.Translate(new Vector3(0 , -0.05f , 0));
         }
     }

     transform.Translate(new Vector3(0 , 0 , speed));
 }

I am trying to get the ball to move up halfway, then move back down once it passed halfway. I am using the magnitude because the ball's and endJumpPosition's rotations are altered. The "HalfwayThere" appears in the console once, and then the ball just keeps going down.

Comment
Add comment · Show 2
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 Exigo95 · May 31, 2019 at 11:41 AM 0
Share

Can you explain what endJumpPosition is actually doing, as I do not understand the usefulness of it, considering it is a vector2/vector3. Do you just want to jump to a certain height or to a specific position?

avatar image martygrof3708 Exigo95 · May 31, 2019 at 03:15 PM 0
Share

Basically there is a path of tiles "square cubes" which the ball follows. Sometimes there is a gap and the player must jump. The endJumpPosition is the position of the next tile. I want it to jump to the specific position that's why I'm not using any force. The vector2 was my attempt at fixing it, as when I just tried to subtract the current ball.z from the endJumpPosition, it didn't work. I thought maybe it's because the rotation of the ball and tiles can change throughout the game.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Exigo95 · May 31, 2019 at 03:55 PM

Your problem is most likely the whole comparison in the if condition, because it doesn't really tell anything. if your player is at (10000, 0, 0) and endJumpPosition is at (10002, 0, 0) you well never be smaller than half of endPosition as he would need o move to a position smaller than 5001 on the x-Axis.

Additionally, you are never setting isJump to false, thus he always executes on side of the if-condition.

You need to store your start jump position and compare your currentToEnd distance to the startToEnd distance. On this way you are only looking at vectors that represent the distance you want to jump.

 bool _IsJump;
 Vector2 _StartJumpPosition;
 Vector2 _EndJumpPosition;
 private void Jump()
 {
     if(Input.GetKey(KeyCode.Space))
     {
         _IsJump = true;
         _StartJumpPosition = new Vector2(transform.position.x, transform.position.z);
     }
 }
 
 private void Move()
 {
     if (_IsJump)
     {
         Vector2 currentJumpPosition = new Vector2(transform.position.x, transform.position.z);
         Vector2 startToEnd = _EndJumpPosition - _StartJumpPosition;
         Vector2 startToCurrent = currentJumpPosition - _StartJumpPosition ;
 
         float startToEndDist = startToEnd.magnitude;
         float startToCurrentDist = startToEnd.magnitude;


     // is the distance to the start smaller than the distance to the end    
         if (startToCurrentDist < startToEndDist / 2)
         {
             Debug.Log("HalwayThere");
             transform.Translate(new Vector3(0, 0.05f, 0));
         }
         else
         {
             transform.Translate(new Vector3(0, -0.05f, 0));
             // did we move further than the end point
             if(startToCurrentDist >= startToEndDist)
             {
                 _IsJump = false;
             transform.position = new Vector3(_EndJumpPosition.x, 0.0f, _EndJumpPosition.y);
             }
         }
     }
     transform.Translate(new Vector3(0, 0, speed));
 }
 

Something along those lines should do it if I understood the situation correct.

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 martygrof3708 · May 31, 2019 at 04:25 PM 0
Share

Thanks, I will try this out. I do reset isJump in OnCollisionEnter when the player hits the tile. Is that bad practice?

avatar image martygrof3708 · May 31, 2019 at 04:27 PM 0
Share

Although I do not really understand what you mean by saying the player will never get halfway there. I've been up all night so please excuse me if its really simple. I am probably just forgetting how the Vectors work.

avatar image Exigo95 martygrof3708 · May 31, 2019 at 07:08 PM 1
Share

The jump reset in OnCollisionEnter is probably fine.

Currently, you are checking if the distance from the world center to the player position is smaller than half the distance to the endpoint. When you are not at the center of the world, this won't work. In the previous example the jump starts at x: 10000 and ends in x: 10002 and as 10000 / 10002 is 0.998 you won't ever enter the condition where you expect this ratio to be below 0.5. To enter the first part of your condition the position of the character would need to be below x: 5000 in this example.

The magnitude of a vector describes its length. In case of a position, which is still a vector, it describes the distance from the center of the world to the position, thus a position vecctor at (10000, 0, 0) has a length of 10000

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

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

rotateAround pivots at unexpected point 1 Answer

Rotation speed,Rotate script slows down 1 Answer

How to get a proper ramming effect? 1 Answer

c# modify only one axis of a quaternion 2 Answers

how to find a point(vector2) between two points(Vector2) of a line (line renderer) 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