Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Rohn_Games · Jul 12, 2017 at 06:19 PM · movementslideicepokemon

How to make a cube slide and can't change direction?

I'm trying to do a movement based on Pokemon Ice Movement / Slider android game.

THIS VIDEO SHOWS THE MOVEMENT: https://www.youtube.com/watch?v=UuvF80xb5Vc

The move consists on when you slide to one axis, the player needs to wait to collide with the wall to have permition to do the next move.

I'm a software engineer student so I'm familiar with programming, but I'm new on Unity and game programming so I need a bit of help right now.



I already have this code:

  public class PlayerController1 : MonoBehaviour {
  

  public float MovementSpeed = 20f;
 
  private bool prevStatusVertical;
  private bool prevStatusHorizontal;
  private bool moveHorizontal;
 
  private CharacterController cc;
  
  void Start () {
      cc = GetComponent<CharacterController> ();
  }
  
  void Update () {
      
      float forwardSpeed = Input.GetAxis("Vertical") * MovementSpeed;
      float sideSpeed = Input.GetAxis("Horizontal") * MovementSpeed;
      
      
      //This variables will be used to check whether player is pressing keys assigned to Horizontal and Vertical
      bool statusHorizontal = Input.GetButton("Horizontal");
      bool statusVertical = Input.GetButton("Vertical");
 
      if (statusHorizontal && !prevStatusHorizontal) 
          moveHorizontal = true;
      if (statusVertical && !prevStatusVertical || !statusHorizontal)
          moveHorizontal = false;
      
      //By default character is not moving
      Vector3 speed = new Vector3 (0, 0, 0);
      
      if (statusVertical && !moveHorizontal)
      {
          speed.z += forwardSpeed;
      }
      else if (statusHorizontal)
      {
          speed.x += sideSpeed;
      }
 
      prevStatusVertical = statusVertical;
      prevStatusHorizontal = statusHorizontal;
      
      cc.SimpleMove (speed);
  }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CheetahSpeedLion · Jul 13, 2017 at 01:56 AM

I would recommend using collision detection and a "touchingWall" boolean whenever the object is in contact with the wall. Then you can do an if( touchingWall ) and put your existing input code inside that.

Areleli

Comment
Add comment · 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
0

Answer by Cornelis-de-Jager · Jul 13, 2017 at 04:51 AM

If you want it to move in one direction until it collides then you con use something like the following. It will work best if there is NO RigidBody component attached:

 Vector3 dir;
 bool canMove = true;

 void Start () {
    // initialise as zero vector
    dir = Vector3.zero;
 }

 void Update () { 
    transform.Translate (dir);
 }
 
 void MoveInDirection (Vector3 _dir) {
     // Check for can move
     if (!canMove)
          return;
     
    // Change Direction
     dir = _dir;

    // Prevent another move until collision
    canMove = false;
 }

 OnCollisionEnter (Collision other) {
     if (other.gameObject.Tag == "wall")  {
      // Set speed to 0
      dir = Vector3.zero;
      canMove = true;
    }
 }


All you need to do then is call the MoveInDirection function each time you want to move. The rest will be handled automatically. Your walls will require the Tag "wall"

Comment
Add comment · 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

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

97 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

Related Questions

Rigidbody Movement 1 Answer

Detect facing normal of trigger mesh? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to Move the Car With Touch? 1 Answer

Object sliding on the roller coaster like track 0 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