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 BitGlitch · Sep 30, 2014 at 02:39 AM · movementraycastraycastinggridgrid based game

Problem with raycast detection?

Ok, so I made a script for grid like movement, and it worked before I started working on adding the obstruction via raycasting. As soon as I added raycasting for obstruction detection, I wasn't getting any errors, but my player is not able to move anymore. I have tried to fix it based on examples, and I have tried this for about a few hours with no luck. Anyone proficient enough in C# to help a new coder like me find the problem? (My code might be a bit of a mess, i just started coding about 2 weeks ago.)

 using UnityEngine;
 using System.Collections;
 
 public class MoveScript : MonoBehaviour {
 
     public bool forwardObstruction = false; // Next few bools are to register if there is an obstruction-
     public bool backwardObstruction = false; // -in the given direction.
     public bool leftObstruction = true;
     public bool rightObstruction = true;
     public float inputDelay = 0.05f; //Stating how many seconds before we can move again.
     public Vector3 forwardDirection; //These next few vectors are direction values.
     public Vector3 backwardDirection;
     public Vector3 leftDirection;
     public Vector3 rightDirection;
     public int smoothFactor = 2; // Not used yet
     public int lineLength = 2; // Not used yet
 
 
     private float timestamp;    
 
         // Update is called once per frame
         void Update () {
 
         // Creates a raycast line forward based on lineLength, checks to see if path is obstructed.
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         if (Physics.Raycast(transform.position, fwd, lineLength)){
             forwardObstruction = true;
         }
             else
             forwardObstruction = false;
     
         
         Vector3 bwd = transform.TransformDirection(Vector3.back);
         if (Physics.Raycast (transform.position, bwd, lineLength)) {
             backwardObstruction = true;
         }
             else
             backwardObstruction = false;
 
         Vector3 left = transform.TransformDirection(Vector3.left);
         if (Physics.Raycast (transform.position, left, lineLength)) {
             leftObstruction = true;
         }
         else
             leftObstruction = false;
 
         Vector3 right = transform.TransformDirection(Vector3.right);
         if (Physics.Raycast (transform.position, right, lineLength)) {
             rightObstruction = true;
         }
         else
             rightObstruction = false;
 
 
             //See if the "Forward" key is pressed
         if (forwardObstruction == true && Time.time >= timestamp && Input.GetButton ("Forward")) {
                         transform.Translate (forwardDirection);
                         timestamp = Time.time + inputDelay;
                         forwardObstruction = forwardObstruction;
                 }
             //See if the "Backward" key is pressed
         if (backwardObstruction == true && Time.time >= timestamp && Input.GetButton("Backward"))  {
                         transform.Translate (backwardDirection);
                         timestamp = Time.time + inputDelay;
                 }
             //See if the "Left" key is pressed
         if (Time.time >= timestamp && Input.GetButton ("Left")) {
                         transform.Translate (leftDirection);
                         timestamp = Time.time + inputDelay;
                 }
             //See if the "Right" key is pressed
         if (Time.time >= timestamp && Input.GetButton ("Right")) {
                         transform.Translate (rightDirection);
                         timestamp = Time.time + inputDelay;
                 }
         }
 }


Any help is appreciated, thank you for at least looking!

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 robertbu · Sep 30, 2014 at 02:54 AM

The problem is that your condition is backwards. Take a look at like 56. You only allow forward movement if 'forwardObstruction' is true. What you really want is:

    if (!forwardObstruction && Time.time >= timestamp && Input.GetButton ("Forward")) {
                     transform.Translate (forwardDirection);
                     timestamp = Time.time + inputDelay;
                     forwardObstruction = forwardObstruction;
     }

Note the way you have things structured, you are doing four Raycast() every frame even when no button is pressed. Consider restructuring things:

    if (!forwardObstruction && Time.time >= timestamp && Input.GetButton ("Forward")) {
         if (Physics.Raycast(transform.position, transform.forward, lineLength)){
                transform.Translate (forwardDirection);
                timestamp = Time.time + inputDelay;
                forwardObstruction = forwardObstruction;
         }
     }

It will greatly simplify your code, and you will only be doing a Raycast() when needed.

Note:

  • transform.TransformDirection(Vector3.forward) same as transform.forward

  • transform.TransformDirection(Vector3.back) same as -transform.forward

  • transform.TransformDirection(Vector3.right) same as transform.right

  • transform.TransformDirection(Vector3.left) same as -transform.right

Comment
Add comment · Show 1 · 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 BitGlitch · Sep 30, 2014 at 02:59 AM 0
Share

Thanks a bunch mate! Gave me a new way to think of this!

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

Touch and Drag 3D Objects on a Grid (for Android) 0 Answers

Edge/Corner Stick — issue or logical error? 0 Answers

Saving Grids in Grid System 1 Answer

Continual in-game directional cursor movement help 0 Answers

raycast in direction of movement key down 2 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