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 awplays49 · Dec 22, 2014 at 03:37 AM · answers

Strange happenings with my code

So im trying to make it so that if my player is above a terrain sample with a new height than the one he jumped on, he wont stop in midair. Here's my code, its somewhere in the jump controls. what do i change to make this possible?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float Speed;
     public float JumpHeight;
     public float JumpSpeed;
     public bool Jumping;
     public float PlayerFoot;
     public float PlayerFootSet;
     public float TerrainHeight;
     public float TerrainHeightSet;
 
     public float Sensitivity;
     private float MouseXSet;
 
     void Start () {
         MouseXSet = 0;
         Jumping = false;
         transform.position = new Vector3 (0, TerrainHeight + transform.lossyScale.y / 2, 0);
     }
 
     void Update () {
         PlayerFoot = transform.position.y - transform.lossyScale.y / 2;
         TerrainHeight = Terrain.activeTerrain.SampleHeight (transform.position);
 
         // Translation controls]
         if (Input.GetKey (KeyCode.W))
         {
             transform.position += transform.forward * Speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.S))
         {
             transform.position -= transform.forward * Speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.D))
         {
             transform.position += transform.right * Speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.A))
         {
             transform.position -= transform.right * Speed * Time.deltaTime;
         }
 
         // Rotation controls
         if (Input.mousePosition.x > MouseXSet)
         {
             transform.Rotate (new Vector3 (0, 1, 0) * (Input.mousePosition.x - MouseXSet) * Sensitivity * Time.deltaTime);
             MouseXSet = Input.mousePosition.x;
         }
         if (Input.mousePosition.x < MouseXSet)
         {
             transform.Rotate (new Vector3 (0, -1, 0) * (-(Input.mousePosition.x - MouseXSet)) * Sensitivity * Time.deltaTime);
             MouseXSet = Input.mousePosition.x;
         }
 
         // Jump controls
         if (Input.GetKey (KeyCode.Space) && Jumping == false && PlayerFoot < TerrainHeight + 1) 
         {
             Jumping = true;
             TerrainHeightSet = TerrainHeight;
         }
         if (Input.GetKeyUp (KeyCode.Space))
         {
             Jumping = false;
         }
         if (Jumping == true)
         {
             if (PlayerFoot < TerrainHeightSet + JumpHeight * 10)
             {
                 PlayerFootSet = PlayerFoot;
                 rigidbody.velocity = new Vector3 (0, (JumpSpeed * 10 / (TerrainHeightSet + JumpHeight * 10 - PlayerFoot)) * JumpSpeed * 3, 0);
             } else {
                 Jumping = false;
             }
         }
         if (Jumping == false)
         {
             if (PlayerFoot > TerrainHeight)
             {
                 Debug.Log ("Here");
                 if (PlayerFootSet < TerrainHeight + JumpHeight * 10)
                 {
                     Debug.Log ("There");
                     rigidbody.velocity = new Vector3 (0, -(JumpSpeed * 10 / (TerrainHeight + JumpHeight * 10 - PlayerFootSet)) * JumpSpeed, 0);
                 }
             } else {
                 rigidbody.velocity = new Vector3 (0, 0, 0);
             }
         }
 
         // Keep player upright
         if (PlayerFoot < TerrainHeight)
         {
             transform.position += Vector3.up * (TerrainHeight - PlayerFoot);
         }
     }
 } 
Comment
Add comment · Show 8
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 Fornoreason1000 · Dec 22, 2014 at 04:19 AM 1
Share

if my player is above a terrain sample with a new height than the one he jumped on, he wont stop in midair

I don't quite understand what you're trying to do. what exactly are you having trouble with?

And what exactly is going wrong?

are you trying to ground a character without using gravity after jumping from a higher point i.e a ledge

alt text

jump1.png (7.4 kB)
avatar image Kiwasi · Dec 22, 2014 at 09:37 AM 1
Share

Dude, put some effort into tags for your question.

avatar image Fornoreason1000 · Dec 22, 2014 at 09:38 AM 0
Share

lol i just noticed that

avatar image Kiwasi · Dec 22, 2014 at 10:17 AM 0
Share

There is some effort goon on behind the scenes to improve the site. Tags are one of the areas we might get cleaned up. In the meantime am asking mods to try and push it. So spread the word.

avatar image awplays49 · Dec 22, 2014 at 02:02 PM 0
Share

@Fornoreason1000 exactly. im trying to make it so that the grounding code applies to whenever he is not jumping. When i tried to make it so that it would also work when he jumps and moves, he stopped in midair. This is because of something in The (if false) jump code. There is a terrain height set so if the player moves uphill, he cant keep floating. Only when he is near grounded will the terrain height set be set.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Mmmpies · Dec 22, 2014 at 04:54 PM

Record the start height (when space is first pressed) and set the maximum jump height to startHeight + jumpHeight. Let that control how high your character can jump rather than the distance from the ground.

Use the terrain height to detect when the jump ends.

If Space is pressed and Jumping == True the you should do nothing handle the jump code in a jumping function and check if(Jumping) in Update to call that function if Jumping == True.

Looks like you're OK at coding but give me a nudge if you want a hand implementing.

If you code like this then even if your character jumps off a cliff they should still fall as they only ever get up to startHeight + jumpHeight.

EDIT:

Well you're setting Jumping to false when you let go of the button. That isn't really true is, the character is still jumping until they land.

It depends on how you want the game to work, do you want the character to jump to the maximum height no matter how long space is pressed or do you want to only keep going up if space is being pressed?

That'll affect how we deal with the code.

Comment
Add comment · Show 40 · 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 awplays49 · Dec 22, 2014 at 05:08 PM 0
Share

Ok thanks :-)

so this is my code now, the only problem left is he still floats up if he moves over a lower terrain while jumping

 if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space) && Jumping == false && PlayerFoot < TerrainHeight + 1) 
         {
             Jumping = true;
             TerrainHeightSet = TerrainHeight;
         }
         if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Space))
         {
             Jumping = false;
         }
         if (Jumping == true)
         {
             if (PlayerFoot < TerrainHeightSet + JumpHeight * 10)
             {
                 PlayerFootSet = PlayerFoot;
                 rigidbody.velocity = new Vector3 (0, (JumpSpeed * 10 / (TerrainHeightSet + JumpHeight * 10 - PlayerFoot)) * JumpSpeed * 3, 0);
             } else {
                 Jumping = false;
             }
         }
         if (Jumping == false)
         {
             if (PlayerFoot > TerrainHeight + 1)
             {
                 if (PlayerFootSet < TerrainHeight + JumpHeight * 10 && PlayerFoot >= PlayerFootSet)
                 {
                     Debug.Log ("1");
                     rigidbody.velocity = new Vector3 (0, -(JumpSpeed * 10 / (TerrainHeight + JumpHeight * 10 - PlayerFootSet)) * JumpSpeed, 0);
                 }
                 if (PlayerFoot < PlayerFootSet)
                 {
                     Debug.Log ("2");
                     rigidbody.velocity = new Vector3 (0, -(JumpSpeed * 10 / (TerrainHeight + JumpHeight * 10 - PlayerFoot)) * JumpSpeed, 0);
             } else {
                 rigidbody.velocity = new Vector3 (0, 0, 0);
             }
         }

avatar image awplays49 · Dec 22, 2014 at 05:15 PM 0
Share

the condition with the "2" in it isnt met when jumping toward a lower surface apparently

avatar image awplays49 · Dec 22, 2014 at 05:45 PM 0
Share

@$$anonymous$$mmpies :-)

avatar image Mmmpies · Dec 22, 2014 at 05:50 PM 0
Share

Got it working?

If so it's good practice to post the working script and mark the question answered, could help someone else. Could even help you if your project screws up for any reason and you need to get the code back.

avatar image awplays49 · Dec 22, 2014 at 06:09 PM 0
Share

its something in the condition that changes the force to move down, though. either something in the jumping == true part or the jumping == false part. @meat5000

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do i delete a stupid question? 1 Answer

Why doesn't Unity Answers work in Firefox? 2 Answers

Attempt at re-arranging list results in crash 1 Answer

I cant shoot the ammo when i trigger the box 1 Answer

Orbiting bodies with unity 5.0 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