• 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 · Mar 26, 2015 at 11:43 PM · jumpingcollision issues

Player is given opportunities occasionally to jump really high, Can't seem to figure out why.

Hi, I am noticing my player jumping hundreds of units up in my game when jumping up blocks (yes blocks its a sandbox game).

I don't know what is causing this at all, but I will tell how I make jumping reoccur.

I tried fixing it by reducing the triggers' sizes, but that didn't help. I will leave a project link below.

1 player jumps 2 player lands 3 the surfaces of meshes are separated as separate meshes, and tagged with trigger colliders as "Block Top" 4 the player has an OnCollisionStay function that recognizes these surfaces

Also, while I'm positing, I'd really like to know a better way to do this. Please comment your suggestions.

https://drive.google.com/file/d/0B2ZiQtohl3tmTE1HaWVtUlVTVW8/view?usp=sharing

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 iagoccampos · Mar 27, 2015 at 04:08 AM

Hey man, I made some alterations in your script, removed the collider that was on the top of the block, created a layer to your block and resized the box collider of the player.

Take a look in the script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float Speed;
     public float LookSpeed;
     public int LookRange;
     public float JumpForce;
     public LayerMask whatIsGround;
 
     private float MouseX;
     private float MouseY;
     public Transform groundCheck;
     private bool grounded;
     private float radius = 0.1f;
     private Rigidbody rb;
 
 
     void Awake()
     {
         rb = GetComponent<Rigidbody>();
         if(!rb)
         {
             Debug.Log("Cant find Rigidbody!");
         }
     }
 
     void FixedUpdate () {
 
         Collider[] whatIHit = Physics.OverlapSphere(groundCheck.position, radius, whatIsGround);
         for(int i = 0; i < whatIHit.Length; i++)
         {
             if(whatIHit[i].CompareTag("Block"))
             {
                 grounded = true;
             }
             else
             {
                 grounded = false;
             }
         }
 
         // WASD Movement
         if (Input.GetKey (KeyCode.W))
         {
             transform.position += transform.forward * Speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.A))
         {
             transform.position -= transform.right * 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;
         }
 
         // Mouse Look
         MouseX += Input.GetAxis ("Mouse X") * LookSpeed * Time.deltaTime;
         transform.rotation = Quaternion.Euler (0, MouseX, 0);
         MouseY -= Input.GetAxis ("Mouse Y") * LookSpeed * Time.deltaTime;
         MouseY = Mathf.Clamp (MouseY, -LookRange / 2, LookRange / 2);
         Camera.main.transform.localRotation = Quaternion.Euler (MouseY, 0, 0);
 
         // Jumping
         if (Input.GetKey (KeyCode.Space) && grounded && rb.velocity.y ==0)
         {
             rb.velocity += Vector3.up * JumpForce;
             grounded = false;
         }
     }
 
     void OnDrawGizmosSelected()
     {
         Gizmos.color = Color.green;
         Gizmos.DrawWireSphere(groundCheck.position, radius);
     }
 }
 

Tip: When do you want your player to react with physics dont move him with transform, later you will see strange movements of your character.

Another Tip: Try to not use GetComponnent inside Update function, for performance reasons.

Ask me anything in the comments, I will try understand(I don't speak english too much) and try explain.

Comment
Add comment · Show 4 · 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 · Mar 27, 2015 at 11:23 AM 0
Share

Hi @iagoccampos!

I was wondering a few things. What should I use ins$$anonymous$$d of transform.position, and what if I wanted something to move but it didn't have a rigidbody?

avatar image iagoccampos · Mar 27, 2015 at 01:35 PM 0
Share

You can use rb.velocity or rb.AddForce ins$$anonymous$$d transform.position, AddForce works great for jump and velocity for movement. Rigidibody is not used only for apply forces in the objects, it used to detect collisions too, if you dont want move your player with physics you need mark "Is $$anonymous$$inematic" checkbox in Rigidbody component in inspector and all movements can be made with transform.position ,but you need create a "gravity" script for your player and I don't think it will be easy.

avatar image awplays49 · Mar 27, 2015 at 06:47 PM 0
Share

But what would I do for a non rigidbody? What if I wanted my enemies to move into me?

avatar image iagoccampos · Mar 28, 2015 at 01:11 PM 0
Share

That is a question that I can't help you... I always use rigidbody when I can... it depends on your game, for example if I'm developing a game like GB pokemons I will never use rigidbody because this type of game don't needs physics interaction and I will use the transform.position to locomotion.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

I have been trying to get a working isGrounded variable to get my character to stop from infinitely jumping can someone please show and/or explain what it is that I am doing wrong. 1 Answer

Jumping doesn't work, and I don't know why. 1 Answer

Jumping Issue, Need Help. 0 Answers

Why can't I properly jump? 2 Answers

How to prevent player from moving in air while jumping in place ?? 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