• 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 zeref_slayer · Dec 24, 2020 at 04:02 AM · scripting beginner

Raycast not working properly

Hi I doing the 2d puzzle game that the player will flip to the ceiling when press the spacebar but the the bool I use was always turn to true every time. I want when the player was grounded than the player can flip and if not the player can't flip. I also want the player can't move when the flip here my code

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerController : MonoBehaviour { float horizontalMove; bool isJumping; RaycastHit2D hit;

 private Transform playerTransform;
 private Rigidbody2D playerRb2d;
 [SerializeField] float speed = 10f;
 PlayerController playerController;

 public float maxRayDistance = 5f;
 public LayerMask whatIsFlip;
 public bool isGrounded;
 public bool isWin;

 // Start is called before the first frame update
 void Start()
 {
     Cursor.lockState = CursorLockMode.Locked;

     playerTransform = GetComponent<Transform>();
     playerRb2d = GetComponent<Rigidbody2D>();
     playerController = GetComponent<PlayerController>();
 }

 // Update is called once per frame
 void Update()
 {
     horizontalMove = Input.GetAxis("Horizontal") * speed;
     isJumping = Input.GetButtonDown("Jump");

     Move();

     if (isJumping && isGrounded)
     {
         playerRb2d.gravityScale *= -1;
     }
 }

 private void FixedUpdate()
 {
     if(Physics2D.Raycast(playerTransform.position, Vector2.down, maxRayDistance, whatIsFlip))
     {
         isGrounded = true;
     }
 }

 void Move()
 {
     if (!isJumping)
     {
         playerRb2d.velocity = (transform.right * horizontalMove) + (transform.up * playerRb2d.velocity.y);
     }

     if (isJumping)
     {
         horizontalMove = Input.GetAxis("Horizontal") * 0;

         playerRb2d.velocity = (transform.right * horizontalMove) + (transform.up * playerRb2d.velocity.y);
     }
 }

 private void OnCollisionEnter2D(Collision2D other)
 {
     if(other.gameObject.CompareTag("Finish"))
     {
         isWin = true;
         playerController.enabled = false;
     }

     else
     {
         isWin = false;
     }
 }

}

hope someone can tell me the mistake that i made

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

121 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 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 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 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 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 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

Move object when player reach trigger 2 Answers

Searching throguh array in custom inspector 0 Answers

Animation while walking 1 Answer

Help! This script is causing my Enemy to die on Spawn?? 2 Answers

How to access Leap motion's bone direction property within C# script ? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges