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 Kanassa · Jan 24, 2021 at 10:48 AM · scripting problemcollisionmovementraycast

My Raycast acts like it's hitting something when it isn't

I'm in the middle of developing a horror game with a grid-based movement system. In order to make collision work, I have the player casting rays in four directions and if the ray hits tagged colliders at a certain distance, then the player can no longer move in that direction.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GridMovement : MonoBehaviour
 {
     //Can't move while active
     public bool isMoving;
 
     //Can't move forward while active
     public bool isMovingForward;
     
     //Can't move back while active
     public bool isMovingBack;
     
     //Can't move left while active
     public bool isMovingLeft;
     
     //Can't move right while active
     public bool isMovingRight;
 
     //Player Position
     public Vector3 origPos, targetPos;
 
     //Transition time
     public float timeToMove = 0.5f;
 
     //Collider range
     public float rayLength = 1.4f;
 
     void Update()
     {
         //Ray
         RaycastHit hit;
 
         //Distance between player and object hit by the Ray
         float theDistance;
 
         //Ray forward
         Vector3 forward = transform.TransformDirection(Vector3.forward) * 2;
         Debug.DrawRay(transform.position, forward, Color.red);
 
         //Ray behind
         Vector3 back = transform.TransformDirection(Vector3.back) * 2;
         Debug.DrawRay(transform.position, back, Color.red);
 
         //Ray left
         Vector3 left = transform.TransformDirection(Vector3.left) * 2;
         Debug.DrawRay(transform.position, left, Color.red);
 
         //Ray right
         Vector3 right = transform.TransformDirection(Vector3.right) * 2;
         Debug.DrawRay(transform.position, right, Color.red);
 
         //if the ray hits an object
         if (Physics.Raycast(transform.position,(forward), out hit))
         {
             theDistance = hit.distance;
 
             //checks the object's tag
             if (hit.collider.tag == "Ground")
             {
                 //checks if ibject hit is too close to the player
                 if (theDistance <= rayLength)
                 {
                     isMovingForward = true;
                 }
 
                 else
                 {
                     isMovingForward = false;
                 }
             }           
                 
         }
 
         if (Physics.Raycast(transform.position, (back), out hit))
         {
             theDistance = hit.distance;
             if (hit.collider.tag == "Ground")
             {
                 if (theDistance <= rayLength)
                 {
                     isMovingBack = true;
                 }
                 else
                 {
                     isMovingBack = false;
                 }
             }           
             
         }
 
         if (Physics.Raycast(transform.position, (left), out hit))
         {
             theDistance = hit.distance;
             if (hit.collider.tag == "Ground")
             {
                 if (theDistance <= rayLength)
                 {
                     isMovingLeft = true;
                 }
                 else
                 {
                     isMovingLeft = false;
                 }
             }            
             
         }
 
         if (Physics.Raycast(transform.position, (right), out hit))
         {
             theDistance = hit.distance;
             if(hit.collider.tag == "Ground")
             {
                 if (theDistance <= rayLength)
                 {
                     isMovingRight = true;
                 }
                 else
                 {
                     isMovingRight = false;
                 }
             }            
             
         }        
     }
 
     private IEnumerator MovePlayer(Vector3 direction)
     {
         isMoving = true;
 
         float elapsedTime = 0;
 
         origPos = transform.position;
         targetPos = origPos + direction;
 
         while(elapsedTime < timeToMove)
         {
             transform.position = Vector3.Lerp(origPos, targetPos, (elapsedTime / timeToMove));
             elapsedTime += Time.deltaTime;
             yield return null;
         }
 
         transform.position = targetPos;
 
         isMoving = false;
     }
 
     //Functions triggered by UI Buttons.
     public void MoveForward()
     {
         if (!isMoving && !isMovingForward)
         {
             StartCoroutine(MovePlayer(Vector3.forward));
         }
     }
 
     public void MoveLeft()
     {
         if (!isMoving && !isMovingLeft)
         {
             StartCoroutine(MovePlayer(Vector3.left));
         }
     }
 
     public void MoveBack()
     {
         if (!isMoving && !isMovingBack)
         {
             StartCoroutine(MovePlayer(Vector3.back));
         }
     }
 
     public void MoveRight()
     {
         if (!isMoving && !isMovingRight)
         {
             StartCoroutine(MovePlayer(Vector3.right));
         }
     }
 }
 

When I test it, it seems to work for the most part, I get too close to an object and can't move in that specific direction. However, if I move away from the object, the bool that restricts my movement doesn't switch off unless I move a certain way. Like, I move to a wall in front of me (a basic cube, 2x2 scale; no extra scripts or components attatched), 'isMovingForward' triggers. I move back, it switches off, if I move to the right, no matter how far right I go and the Raycast clearly isn't hitting anything, it doesn't switch off.

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

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

Box goes through walls 2 Answers

Prevent random movement allowing movement through obstacles 1 Answer

How to move the main camera in Google Cardboard? 0 Answers

Checking for box collider 2 Answers

Issues with Bullet Ricochet 1 Answer


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