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 /
  • Help Room /
avatar image
0
Question by DRsabadash · Feb 09, 2017 at 03:27 PM · vector3character controllerclampz axisx axis

How would i clamp X and Z without clamping Y?

While on the ground, the diagonal movement is clamped to the speed variable, but while in the air, one of two things happen:

either it remains unclamped, and the player can jump normal height but diagonal movement is enlarged ( from speed on x axis + speed on z axis / 2 or whatever the correct math is)

or the whole moveDirection Vector3 is clamped, and the player can jump normal height while stationary, the diagonal is clamped to speed, but the faster the player is going reduces the jump height (player gains less height the more X and Z movement because all 3 variables together cannot exceed the value of speed)

how would i just clamp the X and Z movement while allowing Y to remain unclamped, allowing normal sized jumps without abnormally fast diagonal movement?

as it stands, right now the most efficient way to move in the game is to bunny hop while moving diagonally...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class FPSinput : MonoBehaviour {
 //Public and Private
     public float speed = 9.0f;
     public float gravity = 20.0f;
     public float jumpPower = 8.0f;
     private Vector3 moveDirection = Vector3.zero;
     private CharacterController _charController;
     private Vector3 moveDirectionY = Vector3.zero;
 
 
     // Use this for initialization
     void Start () {
         _charController = GetComponent<CharacterController> ();
 
     }
 
 
 // Update is called once per frame
     void Update () {
         //if shift pressed, set speed to sprint
         if (Input.GetKeyDown (KeyCode.LeftShift)) {
             speed = 18.0f;
         } else if (Input.GetKeyUp (KeyCode.LeftShift)) {
             speed = 9.0f;
         }
         //Checks to see if grounded
         if (_charController.isGrounded) {
             //Movement vector3s
             moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
             moveDirection = Vector3.ClampMagnitude (moveDirection, speed);
             moveDirection = transform.TransformDirection (moveDirection);
             moveDirection *= speed;
             //If space is pressed, jump
             if (Input.GetButton ("Jump")) {
                 moveDirection.y = jumpPower;
             }
         } 
         else {
             //Movement for while not grounded
             moveDirection = new Vector3 (Input.GetAxis ("Horizontal") * speed, moveDirection.y, Input.GetAxis ("Vertical") * speed);
             moveDirection = Vector3.ClampMagnitude (moveDirection, speed);
             moveDirection = transform.TransformDirection (moveDirection);
 
         
         }
         //Apply gravity
         moveDirection.y -= gravity * Time.deltaTime;
         //Movement
         _charController.Move (moveDirection * Time.deltaTime);
 
     }
 }
 



i've tried inserting Mathf.clamp(s) into the else{} moveDirection = new vector for the X and Z parts and it didn't work.

i've also tried assigning the Y movement to a new vector 3 "moveDirectionY" and doing moveDirection = new vector (x, 0, Z) and moveDirectionY = new vector (0, Y, 0), and only clamping moveDirection, but that didn't work (i did put a new _charController.Move = (moveDirectionY * Time.DeltaTime) at the end of Update but it caused all X and Z momentum gained in the air to remain as constant movement on the ground, like sliding without friction)

any help is appreciated, but before anyone suggests it, i'm not switching to Rigidbody.

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 Glurth · Feb 10, 2017 at 06:00 PM

Uncompiled, example only: Check's the movement Vector3's XY component's magnitude against "speed": and if greater, reduces it to "speed"

 float XYmag = Mathf.Sqrt(movement.x*movement.x + movement.y*movement.y);  //good 'ol Pythagorus
 if(XYMag>speed) //if we don't go in here- no need to clamp
 {
    movementNormalizedXY = new Vector3( movement.x/XYmag, movement.y/XYmag, movement.z);  //after this operation the XY component's magnitude will be 1.0f
    movementAtSpeedXY = new Vector3( movementNormalizedXY.x * speed, movementNormalizedXY.y * speed, movement.z); //multiply that 1.0 magnitude by our speed
    movement= movementAtSpeedXY; //done- assign back to movement
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Clamp a Vector3 position to an arbitrary line segment 1 Answer

ClampMagnitude while ignoring a direction 0 Answers

Simple 3d controls 0 Answers

Mouse Controlled Sphere In Front of Character 0 Answers

how to convert a Vector3 to a float value? 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