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 /
This question was closed Sep 24, 2013 at 02:08 PM by Fattie for the following reason:

hopelessly long, no image, will never be answered

avatar image
0
Question by hankphone1 · Sep 24, 2013 at 02:12 AM · movementraycastinputdisablewall

Raycast detects walls, how to disable movement or input in direction of the wall?

Hi All,

I have a simple 2d movement script and a simple raycast script that will detect when the player touches a wall. I have no problem detecting the walls but I am not sure how to disable the player movement in the direction of the wall so that they can not walk through it.

This is a 2d setup similar to old jrpgs, like Final Fantasy for the NES, if that helps to envision the mechanics. Of course, I will need to be able to enable movement toward the wall again once the ray no longer detects the wall, i.e. after the player moves far enough in the opposite direction, but maybe I can figure that part out if someone can help me with disabling.

Movement script:

 var speed: float = 15;
 // Link to the animated sprite
 private
 var anim: tk2dSpriteAnimator;
 
 // This script must be attached to the sprite to work.
 anim = GetComponent(tk2dSpriteAnimator);
 
 
 function FixedUpdate() {
 
     if (Input.GetButton("Horizontal")) {
         rigidbody.velocity = Vector3(speed, 0, 0);
         if (!anim.IsPlaying("right")) {
             anim.Play("right");
         }
         return;
     }
 
     if (Input.GetButton("-Horizontal")) {
         rigidbody.velocity = Vector3(-speed, 0, 0);
         if (!anim.IsPlaying("left")) {
             anim.Play("left");
         }
         return;
     }
 
     if (Input.GetButton("Vertical")) {
         rigidbody.velocity = Vector3(0, speed, 0);
         if (!anim.IsPlaying("up")) {
             anim.Play("up");
         }
         return;
     }
 
     if (Input.GetButton("-Vertical") && Time.time > 1) {
         rigidbody.velocity = Vector3(0, -speed, 0);
         if (!anim.IsPlaying("down")) {
             anim.Play("down");
         }
         return;
     }
 
     if (Input.GetButtonUp("-Vertical")) {
         if (!anim.IsPlaying("idle_down")) {
             anim.Play("idle_down");
         }
     }
 
     if (Input.GetButtonUp("Vertical")) {
         if (!anim.IsPlaying("idle_up")) {
             anim.Play("idle_up");
         }
     }
 
     if (Input.GetButtonUp("-Horizontal")) {
         if (!anim.IsPlaying("idle_left")) {
             anim.Play("idle_left");
         }
     }
 
     if (Input.GetButtonUp("Horizontal")) {
         if (!anim.IsPlaying("idle_right")) {
             anim.Play("idle_right");
         }
     }
 
 }


Raycast script:

 function FixedUpdate () {
  
    var up = transform.TransformDirection(Vector3.up);
    var right = transform.TransformDirection(Vector3.right);
    var hit : RaycastHit;
 
    Debug.DrawRay(transform.position, -up * 0.3, Color.green);
    Debug.DrawRay(transform.position, up * 0.9, Color.green);
    Debug.DrawRay(transform.position, -right * 0.9, Color.green);
    Debug.DrawRay(transform.position, right * 0.9, Color.green);
  
    if(Physics.Raycast(transform.position, -up, hit, 0.3)){
       Debug.Log("Hit");    
       if(hit.collider.gameObject.name == "BottomWall"){
            //Disable movement or input here
       }
    }
 
     if(Physics.Raycast(transform.position, up, hit, 0.9)){
       Debug.Log("Hit");    
       if(hit.collider.gameObject.name == "TopWall"){
            //Disable movement or input here
       }
    }
 
     if(Physics.Raycast(transform.position, -right, hit, 0.9)){
       Debug.Log("Hit");    
       if(hit.collider.gameObject.name == "LeftWall"){
            //Disable movement or input here
       }
    }
 
     if(Physics.Raycast(transform.position, right, hit, 0.9)){
       Debug.Log("Hit");    
       if(hit.collider.gameObject.name == "RightWall"){
            //Disable movement or input here
       }
    }
 }

Thanks!

Comment
Add comment · Show 2
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 Fattie · Sep 24, 2013 at 02:08 PM 0
Share

(1) nobody will, ever, read that much code. (2) just include a SCREEN SHOT so people know what is going on.

To help, I've delete this question - ask a new one or you'll never get an answer. Cheers

avatar image oliver-jones · Sep 24, 2013 at 02:08 PM 1
Share

Couldn't you just use the Collision system built within Unity?

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Prevent player from moving in a certain direction while moving in another 1 Answer

Prevent Player from moving into opposite direction than he already is 1 Answer

Mouse Input for RayCast - Update or FixedUpdate 1 Answer

How to Move a Cube 3 Answers

Stopping an object immediately 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