Move X Velocity without bothering Y velocity

Below is a script I have recently put in to allow the player to move on the x-axis. The problem is when the player is in the air and you use the movement keys, they fall at a noticeably slower rate. How do I have it so that the velocity moves on the x-axis without bothering the y velocity?

if(Input.GetKey("d"))
		{
			rigidbody.velocity = transform.right*speed;
			//rigidbody.velocity.x=speed;
		}
		if(Input.GetKey("a"))
		{
			rigidbody.velocity = transform.right*-speed;
			//rigidbody.velocity.x=-speed;
		}
		if(Input.GetKeyUp("d"))
		{
			rigidbody.velocity.x = 0;
		}
	
		if(Input.GetKeyUp("a"))
		{
			rigidbody.velocity.x = 0;
		}

You might have to find the script that handles the y movement.
When you find the script that is handling the y movoment, you can place an if statment for the y movement checking to see if your PC is jumping or not. (if not jumping then adjust y).

if statments are programming syntex, you’ll find it inside the manual on scripts.

This doesn’t help unfortunately. I know it’s the rigidbody.velocity statements that are messing up the player’s fall rate. I just don’t know how to have the movement set to transform.right without it bothering its Y velocity. If I used the code that was commented out it would work fine but that code doesn’t move the player on its local axis.

Hello topaz!

In order to achieve this functionality you have to work with character controller instead of working with physics.
I once had the same kind of requirement while developing a game.
It was developed for iOS but you can replace the touch with your own button code.
Here is the code:

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(CharacterController))]
public class moveTank : MonoBehaviour
{    
    Transform tank;
    public static float speed = 7.0f;//Speed by which tank is moving
    Vector3 temp;
    public float jumpSpeed = 5.0f;
    CharacterController ch;
    Vector3 movement=Vector3.zero;
    Vector3 velocity = Vector3.zero;
    public Transform refer;

    // Use this for initialization
    void Start()
    {
        //Caching our gameObject tank so that we don't have to look for it in every frame
        tank = this.transform;        
        ch = tank.GetComponent(typeof(CharacterController)) as CharacterController;
    }

    // Update is called once per frame
    void Update()
    { 
            movement = tank.TransformDirection(Vector3.forward);
            movement *= speed;


            // We only want horizontal movement
            movement.y = 0;        
            
        
            // Check for jump
            if (ch.isGrounded)
            {
                foreach(Touch touch in Input.touches)
                {
                    if(touch.phase==TouchPhase.Began)
                    {
                        if(touch.tapCount==2)
                        {
                            velocity.y = jumpSpeed;            
                        }
                    }
                }//end of for
            
                if (Input.GetButtonDown("Jump"))
                {
                    // Apply the current movement to launch velocity            
                    velocity.y = jumpSpeed;
                }
            }

            else
            {
                // Apply gravity to our velocity to diminish it over time
                velocity.y += Physics.gravity.y * Time.smoothDeltaTime;
            }

            movement += velocity;

            // Actually move the character    
            ch.Move(movement * Time.smoothDeltaTime);
        
    }//end of Update
    
}

Hope this would help you.

For this game, we need the player to be affected by physics so we can’t use the character controller. Unless you know another way to move the player along the x-axis besides transform.translate, I’m still looking for how to get it to leave the y velocity alone.

Ok then did you try MovePosition method of Rigidbody? By that way your rigidbody will be affected by physics along with that you can move it in any direction.

Thank you, unless I find a bug or something, it looks like this code got the job done.

if(Input.GetKey("d"))
		{
			//rigidbody.velocity = transform.right*speed;
			rigidbody.MovePosition(rigidbody.position + transform.right*speed*Time.deltaTime);
			//rigidbody.velocity.x=speed;
		}
		if(Input.GetKey("a"))
		{
			//rigidbody.velocity = transform.right*-speed;
			rigidbody.MovePosition(rigidbody.position + transform.right*-speed*Time.deltaTime);
			//rigidbody.velocity.x=-speed;
		}

Nevermind, I found that by doing this the player was unable to move on a moving platform. It also caused the same jittery effect when moving against an added force when using transform.translate.

I didn’t get you. Are you still getting jittery effect after using MovePosition?
Do not use the transform component to move the player when you want it to be affected by physics. Thats why i suggested you to use MovePosition method of rigidbody. And i assume that you are applying your code inside FixedUpdate as it is the only function provided to get physics simulation for rigidbodies.

Why not try to have CharacterController as well as rigidbody on your player. Thereby you can move the player without any jerky movement by using the code which i have posted above and that too along with the physics simulation. Just apply the gravity once i.e., inside the Character Controller move script.

The first problem is that I don’t know C#. The other reason I haven’t used the character controller is because it causes all sorts of problems once applied. The most troublesome is that it sometimes moves through entire objects and I can’t get it to have the controls I want.

I’m looking to make a platformer with pinpoint controls. I actually could get it to move on the x-axis without bothering the y-velocity however when the player would move down slopes, it would have this noticeable drop down effect, even when I rotated the player to always have it’s x-axis parallel to the ground. However in order to have velocity move on the axis and not in world space, I haven’t been able to do it without bothering the y velocity.

bump

I know “me too” posts aren’t helpful. But I am watching this thread too and would find the answer useful too. thanks :slight_smile: