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 Suraj. · Mar 28, 2015 at 05:13 AM · movementraycasttileraycasthit2dmaze

Need help with raycast movement.

Hi. So I am making a maze game for practice. Using a maze generator script. I am moving my character with arrow keys using simple movement script.On android it moves with a swipe(left,right,up,down).Now what I want is that once a finger is swiped, the player(character in game) should move continuously making all possible turns till it reaches a point where it needs instruction from user to turn(suppose it reaches a point where there are 2 turns left and right).But if there is dead end ahead and space on right side,it should automatically turn right(90degree). I want it to be like a android game 'Maze King'. I got to know raycast would be nice and easy way to do it..I have never used raycast. Any help regarding same would be much appreciated. Below is script....character is moving and rotating left and up(with arrow keys) just fine. HOwever not all perfect

using UnityEngine; using System.Collections;

public class Move : MonoBehaviour {

 // Use this for initialization
 public float speed = 0.3f;
 Vector2 dist = Vector2.zero;
 public Quaternion lookLeft = Quaternion.Euler(0, 0, 0);
 public Quaternion lookRight = Quaternion.Euler(0, 0, 0);
 public Quaternion lookTop = Quaternion.Euler(0, 0, 0);
 public Quaternion lookDown = Quaternion.Euler(0, 0, 0);
 RaycastHit2D raycastgo;
 RaycastHit2D raycastleft;
 RaycastHit2D raycastright;
 RaycastHit2D raycastdown;
 public bool movementbool = false;
 public bool movementbool2 = false;
 public bool movementbool3 = false;
 public bool movementbool4 = false;
 public GameObject Player;
 public Transform player;


 void Start () {
     dist = transform.position;
     raycastgo = Physics2D.Raycast(transform.position, Vector2.up, 10, 1 << LayerMask.NameToLayer("MazeBox"));
     raycastleft = Physics2D.Raycast(transform.position, -Vector2.right, 10, 1 << LayerMask.NameToLayer("MazeBox"));
     raycastright = Physics2D.Raycast(transform.position, Vector2.right, 10, 1 << LayerMask.NameToLayer("MazeBox"));
     raycastdown = Physics2D.Raycast(transform.position, -Vector2.up, 10, 1 << LayerMask.NameToLayer("MazeBox"));
 }    

 // Update is called once per frame
 void Update(){


 }

 void FixedUpdate () {

     Vector2 p = Vector2.MoveTowards(transform.position, dist, speed);
     rigidbody2D.MovePosition(p);
     if ((Vector2)transform.position == dist) {

         
         if(!raycastright || !raycastgo || !raycastleft || !raycastdown || !raycastright && !raycastgo || !raycastright && !raycastleft && !raycastgo || !raycastleft && !raycastgo && !raycastdown || !raycastright && !raycastleft && !raycastdown || !raycastright && !raycastleft || !raycastgo && !raycastdown || !raycastleft && !raycastgo || !raycastdown && !raycastleft || !raycastright && !raycastgo || !raycastright && !raycastdown){
             Takeinput();
         }
         if(movementbool==true && !raycastgo && valid(Vector2.up)){
             dist = (Vector2)transform.position + Vector2.up;
             transform.rotation = lookTop;
             movementbool = false;
         }
         if(movementbool3==true && !raycastdown && valid(-Vector2.up)){
             dist = (Vector2)transform.position - Vector2.up;
             transform.rotation = lookDown;
             movementbool3 = false;
         }
         if(movementbool4==true && !raycastleft && valid(-Vector2.right)){
             dist = (Vector2)transform.position - Vector2.right;
             transform.rotation = lookLeft;
             movementbool4 = false;
         }
         if(movementbool2==true && !raycastright && valid(Vector2.right)){
             dist = (Vector2)transform.position + Vector2.right;
             transform.rotation = lookRight;
             movementbool2 = false;
         }

     }
 }
 
 bool valid(Vector2 dir) {
         Vector2 pos = transform.position;
         RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
         return (hit.collider == collider2D);
     }

 public void Takeinput()
 {    
     if (Input.GetKey(KeyCode.UpArrow)){
             movementbool = true;
         }
     if (Input.GetKey(KeyCode.RightArrow)){
             movementbool2 = true;
         }
     if (Input.GetKey(KeyCode.DownArrow)){
             movementbool3 = true;
         }            
     if (Input.GetKey(KeyCode.LeftArrow)){
             movementbool4 = true;
         }
 }

}

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

Answer by abi-kr01 · Mar 28, 2015 at 05:29 AM

if i am understanding it rit you need to add constant movement to player ones user swap . like in a temple run player will keep on running and your swap give a direction to run . You can add relative velocity to your player it will be constant and depends on you play transform .

Comment
Add comment · Show 3 · 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 Suraj. · Mar 28, 2015 at 09:30 AM 0
Share

thanks for ur reply...yes the character has to move at constant speed.Lets say its in maze and it reaches a point where there is dead end ahead and wall on left and right open.so it should turn to right by itself. User should get the option to swipe for turn only if there are more than 1 turn possible.."maze king" is a android game based on this type of movement.

avatar image abi-kr01 · Mar 28, 2015 at 03:17 PM 0
Share

use four raycast from the player pos the one which don't have hit

avatar image abi-kr01 · Mar 28, 2015 at 03:18 PM 1
Share

alt text

untitled.png (23.2 kB)

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

21 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

Related Questions

Raycast based tile navigation 0 Answers

Raycasting in 2D to prevent a player from moving through walls? 1 Answer

Trying to get a 2D RayCast to instantiate a GameObject 0 Answers

Push an object opposite direction of mouse position 0 Answers

Problem with raycast vertical 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