- Home /
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;
}
}
}