• 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 1Crio · Nov 03, 2015 at 05:43 PM · movementplayersideways

How can i block sideways movement?

How can i block sideways movement when the player hits a cube? I need only to move on z and x axis.alt text

immagine.png (14.4 kB)
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 Eno-Khaon · Nov 03, 2015 at 06:22 PM 0
Share

To be able to provide an adequate answer, some more information would be helpful.

To give a few examples of what you could provide:

Is your game 3D or 2D (in Unity)? Each uses different functions for the same general purposes, so an answer can be better tailored to your needs by knowing what you're currently doing.

How does your player currently move? Are you using physics interactions, such as a rigidbody's velocity or AddForce()? Are you directly changing your player's position using transform.position?

If you are using transform.position to update your player's location, are you currently checking for any objects in your player's path?

If you are already doing any of this (or even if now, really), then by all means, post some of your code so we know where you stand and can offer assistance which can be directly relevant. People don't generally want to create an entire script for someone who, for all they know, have done nothing for themself.

avatar image meat5000 ♦ · Nov 03, 2015 at 07:14 PM 1
Share

Put a constraint on the rigidbody.

Add more info.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dray · Nov 03, 2015 at 08:55 PM

Intuitively I'd say just take the value that defines your movement before it get's applied to your GameObject and just "cut" off the value of the axis you want to block. Be it the smaller value, this is an example of what I mean:

 //where ever this comes from.. could be your rigidbodies velocity or something
 Vector2 velocity = new Vector2(2.0f, 1.0f);
 
 if (velocity.x > velocity.y) {
     velocity.y = 0;
 } else {
     velocity.x = 0;
 }

Put this in LateUpdate and you're good to go I think

Comment
Add comment · Show 2 · 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 meat5000 ♦ · Nov 03, 2015 at 09:01 PM 0
Share

I was going to suggest something similar:

 void FixedUpdate ()
 {
     if(rb.velocity.x > rb.velocity.z)
     {
         rb.velocity = new Vector3(rb.velocity.x, 0.0f, 0.0f);
     }
     else if(rb.velocity.z > rb.velocity.x)
     {
         rb.velocity = new Vector3(0.0f, 0.0f, rb.velocity.z);
     }
     
     rb.velocity = new Vector3(rb.velocity.x, 0.0f, rb.velocity.z);
 }

But I dont think it'll really work.

avatar image 1Crio · Nov 04, 2015 at 05:15 PM 0
Share

That worked` public Rigidbody rb;

 void OnCollisionEnter(Collision other)
 {
     if (rb.velocity.x > rb.velocity.z) 
     {
         rigidbody.constraints = RigidbodyConstraints.FreezePositionZ | 
                                 RigidbodyConstraints.FreezeRotationX | 
                                 RigidbodyConstraints.FreezeRotationY | 
                                 RigidbodyConstraints.FreezeRotationZ;
     }

     if (rb.velocity.z > rb.velocity.x) 
     {
         rigidbody.constraints = RigidbodyConstraints.FreezePositionX | 
                                 RigidbodyConstraints.FreezeRotationX | 
                                 RigidbodyConstraints.FreezeRotationY | 
                                 RigidbodyConstraints.FreezeRotationZ;
     }
 }`

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

35 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

Related Questions

How to Logically Match Ground Slope While Using This Code? 1 Answer

Move player in a cyclic path? 0 Answers

How can I convert LOCAL transform.forward to WORLD Vector3? 2 Answers

why is my cube moving sideways in inconsistent distances? 2 Answers

Have a character face their moving direction? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges