Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
-1
Question by CyanGames · Apr 16, 2018 at 11:42 PM · movementcamera-movementy-axiswasd

How to keep player from going below the floor?

Hey, so I created a script for a first person game easy enough, and it basically moved the player whenever they pressed the move keys. However, I wanted it so that it would move wherever the player was looking. But, I don't want the player's y axis. And since my current code takes in x, y, and z, it's letting me move through the floor and all that bad stuff. Here's my code for the movement of the player:

 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour {
     public float speed = 6f;
     Vector3 movement;
     Rigidbody playerRigidbody;
     int floorMask;
     float camRayLength = 100f;
 
     private void Awake() {
         floorMask = LayerMask.GetMask("Floor");
         playerRigidbody = GetComponent<Rigidbody>();
     }
     private void FixedUpdate() {
         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D)) {
             float horizontal = Input.GetAxisRaw("Horizontal");
             float vertical = Input.GetAxisRaw("Vertical");
 
             Move(horizontal, vertical);
         }
     }
     private void Move(float horizontal, float vertical) {
         movement.Set(horizontal, 0f, vertical);
         // Normalize speed so they don't go faster by going diagonally
         // movement = movement.normalized * speed * Time.deltaTime;
         transform.position += transform.forward * Time.deltaTime * speed;
         playerRigidbody.MovePosition(transform.position + movement);
     }
 }

In summary of this code, it basically fires Move() when the player pressed ASWD (if it didn't, it would constantly move the player where they were looking, just because of how this is set up). So, firstly, any suggestions, and secondly, how can I keep the y from changing?

Comment
Add comment · Show 3
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 NonsenseSynapse · Apr 17, 2018 at 12:46 AM 0
Share

To clarify, is the issue that your character is falling through a totally flat floor, or that you have inclines and declines and your character is walking through those?

avatar image CyanGames · Apr 17, 2018 at 01:13 AM 0
Share

@NonsenseSynapse It's neither. The player isn't falling through the floor, but it is able to, because the y changes depending on the mouse location.

avatar image sean244 · Apr 17, 2018 at 01:31 AM 0
Share

Why do you have a “vertical” float if you don’t want the player to move along the y-axis?

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by MechaWolf99 · Apr 17, 2018 at 02:11 PM

I can't say for sure. But could do two things. One, if the player will always be at the same height, then make a new vector3 and set it's x, and z to be the same as transfrom.position, and set y to the level of the floor. The other option that I can think of is to make a new vector3, set it to be the same x,y,z of transfrom.position. But for y, subtract the new position form it. Then of course set transfrom.position to be the vector3. Hope this helps. If not then I can try to write some code up for you.

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

125 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 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

Vertical & horizontal movement world axis (top view) 1 Answer

how to stop something from following in the y axis 1 Answer

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

Camera Movement 0 Answers

Need help with WASD plus mouselook movement,,Need help with WASD movement 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