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 Mman1235 · Aug 24, 2015 at 02:02 PM · scripting problembeginnerphysics2d

groundCheck in small beginner project not working properly

Hi, I am working in my free time on a small project to get the hang of unity.

In the game, the player can click anywhere on screen and is teleported there instantly. When he falls and hits the floor (which isn't rendered and only has a transform and collider2d) he is supposed to slide back to his original position (around the bottom left corner of the screen).

This movement script I am working on before continuing on the next parts of the game is almost done, but it only has 1 small issue now.

When the player falls, he hits the ground, but the sliding back doesn't happen, since the groundCheck (a transform childed onto the player) from what I am guessing isn't registering that the groundCheck is at the floor... The only way the sliding back happens is when I teleport the character very close to the floor.

I would be very happy if someone could give an idea or a thought on how can I approach this issue which after many different methods I can't get to fix, here's the code, thanks!:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour
 {
     public float moveTime = .5f;
 
     //public GameObject groundCheck; //will use to check if grounded once ground colliders and physics settled
     public GameObject WholePlayer; //will take in the full sprite for warping mechanics
     public GameObject groundCheck; //will be used to check if currently running on ground
     public Vector3 Final_Run_Spot;
 
     public Material In_Air;
     public Material On_Ground;
     public Material Dead_1;
     public Material Dead_2;
     public Material Warped;
 
 
     public float Final_Spot_Return_Speed = 1f;
 
 
     private bool grounded = false;
     private Rigidbody2D rb2d;
     //private Material Player_Color;
     //private Animator anim;  //will be added when sprites implemented on working version
 
     private float inverseMoveTime;
 
     private IEnumerator sm;
 
     void Awake()
     {
         //anim = GetComponent<Animator>();
 
         rb2d = GetComponent<Rigidbody2D>();
         //Player_Color = GetComponent<Material>();
         //Player_Color = In_Air;
         WholePlayer.GetComponent<Renderer>().material = In_Air;
 
         //By storing the reciprocal of the move time we can use it by multiplying instead of dividing, this is more efficient.
         inverseMoveTime = 1f/moveTime;
 
         sm = SmoothMovement();
     }
 
 
     // Update is called once per frame
     void Update () 
     {
         Vector2 currentPosition = transform.position;
         grounded = Physics2D.Linecast(transform.position, groundCheck.transform.position, 1 << LayerMask.NameToLayer("Ground")); 
 
         if(grounded)
         {
             Debug.Log("Grounded"); 
 
             //transform.position = Vector3.Lerp( currentPosition, Final_Run_Spot, Time.deltaTime );
             StartCoroutine(sm); //SmoothMovement());
             Debug.Log("Smoooooth");
         }  
         if(!grounded)
             StopCoroutine(sm);
 
 
         
 
         ClickCheck();
     }
 
     void FixedUpdate()
     {
         rb2d.gravityScale = 0.85f;
     }
     
 
     void ClickCheck() // Warping on screen
     {
 
         if (Input.GetButtonDown("Fire1")) //If left clicked
             {
             Vector3 mousePos = Input.mousePosition; //takes pixel locations
             mousePos.z = 1; //very important! mouseposition z axis is based around camera meaning z=0 is camera z!!!
             Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
             WholePlayer.transform.position = new Vector3(Mathf.Clamp(worldPos.x,-6.6f,6.55f), Mathf.Clamp(worldPos.y,-3.7f,3.8f), worldPos.z);                   
             //clamping 2d axises to not let playr warp on an area outside camera view (also outside current generated room)
 
             rb2d.gravityScale = 0f; //reseting ravity so warps won't continue falling velocity - gravity still affecting player!
 
             grounded = false;
             Debug.Log("Not grounded");
             }
     }
 
 
     IEnumerator SmoothMovement()
     {
 
         //Calculate the remaining distance to move based on the square magnitude of the difference between current position and end parameter. 
         //Square magnitude is used instead of magnitude because it's computationally cheaper.
         float sqrRemainingDistance = (transform.position - Final_Run_Spot).sqrMagnitude;
         //While that distance is greater than a very small amount (Epsilon, almost zero):
         while(sqrRemainingDistance > float.Epsilon)
         {
             //Find a new position proportionally closer to the end, based on the moveTime
             Vector3 newPostion = Vector3.MoveTowards(rb2d.position, Final_Run_Spot, inverseMoveTime * Time.deltaTime);
             
             //Call MovePosition on attached Rigidbody2D and move it to the calculated position.
             rb2d.MovePosition (newPostion);
             
             //Recalculate the remaining distance after moving.
             sqrRemainingDistance = (transform.position - Final_Run_Spot).sqrMagnitude;
             
             //Return and loop until sqrRemainingDistance is close enough to zero to end the function
             //if(grounded)  //NEED TO FIX GROUNDED AND MOVEMENT PROBLEM
             //if(!grounded)
                 //StopCoroutine(sm);
                 //yield break;
             yield return null;
 
     }
     }
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

[Newbie] Particle System not playing, C# (unity 5.5) 3 Answers

I really need a JumpPad script for a JumpPad in unity 2D 0 Answers

Component goes away on play? 1 Answer

a way to predict players position to be able to be hit by projectiles 0 Answers

A Proper Way To Pause A Game 9 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