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 Sawula · Apr 04, 2015 at 02:19 PM · c#positionjumpcompare

C# store compare start position and actual position

Hello guys, I'm trying to make a script for a character to jump up to a certain height, then fall down. I'm using gravity to make it easier, but I'm having a little trouble finding how to limit the jump height.

I tried storing the starting y position of my player, then making a variable (toppos) that would be equal to 5 time the starting y as a limit. Then I want to compare the current y to that limit value and when they're equal the jump is off, and then when current y is equal to starting y again the jump can be used again. Still I must have done something wrong since no matter what I do and how high I go, the jump never goes off.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody))]
 public class Simple2DMoveController : MonoBehaviour {
 
     public KeyCode Left = KeyCode.LeftArrow;
      public KeyCode Right = KeyCode.RightArrow;
      public KeyCode Up = KeyCode.UpArrow;
      public KeyCode Down = KeyCode.DownArrow;
 
     private Vector3 startPos;
 
     private Vector3 toppos;
 
      public float speed;
      [HideInInspector]public float currentSpeed;
 
     void Start () {
 
     currentSpeed = speed;
     rigidbody.constraints = RigidbodyConstraints.FreezePositionZ|RigidbodyConstraints.FreezeRotation;
     }
 
     //to set starting position when touching a platform
     void OnCollisionEnter (Collision col){
         if(col.gameObject.tag == "ground") startPos = transform.position;
     }
 
     void FixedUpdate () {
 
         //bool to activate jump or no, declaration of toppos and check
         bool canjump = true;
         toppos = new Vector3 (startPos.x, startPos.y + 4, startPos.z);
         Debug.Log (toppos);
 
     rigidbody.velocity = Vector3.zero;
         if(Input.GetKey(Left)){ rigidbody.AddForce(-Vector3.right * speed, ForceMode.VelocityChange);
             }
         if(Input.GetKey(Right)){ rigidbody.AddForce(Vector3.right * speed, ForceMode.VelocityChange);
             } 
         //check if canjump is true
         if (canjump){
             if(Input.GetKey(Up)){ rigidbody.AddForce(Vector3.up * speed, ForceMode.VelocityChange);
                 //supposed to set canjump to false when toppos is reached
                 if (transform.position == toppos) canjump = false;}
             }
         //supposed to set canjump to true once ground is touched again
         if (transform.position == startPos) canjump = true;}
 }
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
Best Answer

Answer by britsplease · Apr 04, 2015 at 03:31 PM

The answer to your question is that you should also check that your character jumped higher than the limit. This is because it is very likely that there will be no frame where your character jumped exactly 4 units, it will be under it in one frame and in the next frame it will be above it, in most cases. So use transform.position.y >= toppos.y.

On the other hand, if use check whether the ground is touched by transform.position == startPos, then your character will be able to jump again only if it didn't move sideways and fell back to the exact position. The reason is that this way you check each coordinates of the Vector3 position. For height you should only check the y coordinate.

Also your idea of saving the starting height for checking whether the player is on the ground is not really robust and works only when your map is flat. For example when you jump up to a platform, you won't be able to jump again since the coordinates won't match. Using raycasting would be a better idea.

Comment
Add comment · Show 1 · 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 Sawula · Apr 04, 2015 at 05:00 PM 0
Share

I modified the script as you said, and it worked, thanks. I wouldn't know about raycasting, $$anonymous$$cher haven't taught us those yet (I'm in a video game school) He gave us this assignment to see if we could do it and how, and he told us he'll give us the real solution next class.

It might be raycasting XD

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

20 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

If gameobject moves do this 1 Answer

How can i make my enemy jump with a delay 1 Answer

Game Object to Transform Position 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