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
1
Question by modernmenace · Apr 23, 2014 at 08:08 PM · movementcharacter controlling

Free Movement (Space/Underwater) With First Person Controller

Hey guys, I have spent the past few weeks stumped trying every scrip and answer trying to get this to work. I am trying to get the first person controller to be able to move up and down along with the default sideways/forwards/backwards. Can anyone help me out? Thanks.

Comment
Add comment · Show 1
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 ToxxicSin · Apr 24, 2014 at 02:25 AM 0
Share

Well I would assume you would make a bool of being in the water, just like having a boolean for the isGrounded boolean. And then, if the player is inWater, set the gravity of the player to whatever you would wish. And also, if the player is inWater, and if the spacebar is held, add an upwards force, else if the player is inWater but spacebar is not held, apply a downwards force.

The problem with the First Person Controller is the lack of a rigidbody. I'm not an expert on the First Person Controller, but from what I understand, all of its physics are controlled by the scripts and barely by the world physics. Since it has no rigidbody, functions such as "rigidbody.AddForce()" cannot be used. $$anonymous$$y recommendation would be to make your own character controller that uses a rigidbody, that way you can use things such as the rigidbody.AddForce(). If you would like some help with this, I would be glad to assist you

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by chrisall76 · Apr 24, 2014 at 05:11 AM

You would have a first create a new controller based off of rigidbodies, as the regular one is weak for anything other than what it was made for (plus, coding your own is better always). As said, you should have a bool for if the player is underwater. If he is, his jump (using AddForce) with happen as long as your underwater. I'll help you as making your own controller feel nice takes a bit of testing.
(this is based off my own controller, unlike mine though this doesn't have a separate script for just taking in input)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     private bool _Jump;
     private bool _Crouch;
     private bool Running = false;
     private float Speed = 0;
 
     [HideInInspector]
     public bool losingStamina = false;
     [HideInInspector]
     public bool canJump = true;
     [HideInInspector]
     public bool isMoving = true;
     [HideInInspector]
     public bool isGrounded = true;
     [HideInInspector]
     public bool isUnderWater = true;
 
     public float walkSpeed = 10;
     public float runSpeed = 15;
     public float AirModifier = 1.5f;
     public float fakeFriction = 0.86f;
     public float jumpForce = 40;
     public float isGroundCheck = 1;
 
     void Update () {
         //Speed Modifier
         if (Running == true && isMoving && isGrounded) {
             Speed = runSpeed;
         } else if (Running && isMoving && isGrounded) {
             Speed = walkSpeed;
         } else{
             Speed = walkSpeed/AirModifier;
         }
 
         isGrounded = Physics.Raycast(transform.position, -Vector3.up, isGroundCheck);
         Running = Input.GetKey(KeyCode.leftShift);
         _Jump = Input.GetKeyDown(KeyCode.Space);
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
 
         if(h >= 0.1 || h <= -0.1 || v >= 0.1 || v <= -0.1 || _Jump){
             isMoving = true;
         }else{
             isMoving = false;
         }
 
         //Limit Speed
         if(rigidbody.velocity.magnitude > Speed){
             rigidbody.velocity = rigidbody.velocity.normalized * Speed;
         }
 
         //Movement
         this.rigidbody.velocity = new Vector3 (this.rigidbody.velocity.x * fakeFriction, this.rigidbody.velocity.y, this.rigidbody.velocity.z * fakeFriction);
         Vector3 MovementForce = new Vector3 (move.x, 0, move.z);
         MovementForce.Normalize ();
         Vector3 MoveForce = transform.TransformDirection (MovementForce);
 
         if (Moving) {
             rigidbody.AddForce (MoveForce * Speed);
         }
 
         if (isGrounded && canJump && _Jump || isUnderWater) {
             rigidbody.AddForce(transform.up * jumpForce, ForceMode.Impulse);
         }
     }
 }
Comment
Add comment · Show 1 · 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 Teseu · Jan 17, 2018 at 01:12 PM 0
Share

Where does the "move" var?

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

25 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

Related Questions

pivot of CharacterController`s collider is not the pivot of the charcter???? 0 Answers

How to move character from one room to another? 1 Answer

Controllers standard assest script problems 0 Answers

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

Making a bubble level (not a game but work tool) 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