- Home /
Move only x axis
Hi, how can I move my character only in x axis?...I have character controller and rigidbody on my player and now I want to add script that allows to move only in x-axis...I don't want to make my character rotate only move in X-axis.
do you want automatic walk by the player or by key controll
Answer by FLASHDENMARK · Dec 08, 2011 at 05:41 PM
You did not provide any real information other than you want it to move in the X-axis-direction, so I'll have to work with what I got.
var moveSpeed = 5.0;
function Update (){
transform.position.x += moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
}
Very simple and basic, but maybe that'll be enough.
It works when I don't have rigidbody...when I have rigidbody on my character then it falls through the terrain...
What I am tring to do is that my character has rigidbody and character controller and I want to move character only in x-axis...Like FPScontroller only whitout z-axis
Using transform.postion blah blah, will make your object phase through all objects in the scene. If you are using a rigidbody you should use Rigidbody.AddForce(transform.right * 2500);
Or something similar.
Answer by funkyllama · Dec 08, 2011 at 06:04 PM
If you already have your script that moves the character and you want to lock rotation axis and/or movement axis, then you can do this on the rigidbody itself within the inspector window. Rigidbody has constraints built in, so if you want to completely freeze any rotation & only allow movement on x, tick x, y & z in freeze rotation and tick y & z in freeze position. If it's a script you're after then the simple one OrangeLightning provided will do the trick.
I don't want to really freeze y and z...I only want character to move x-axis...I still need Y and z axis.
Answer by Andyvege · Feb 02, 2014 at 12:25 PM
So, i also want this (only allow the character to move along the x AND y axis.) but i add Rigidbody, and i open constraints and nothing happens when i check those boxes?!
Your answer
Follow this Question
Related Questions
how to not move on the x axis while not jumping 2 Answers
How do I stop a velocity only on the X axis? 2 Answers
my floating spaceship(charactercontroller) won't move in x direction... 1 Answer
3rd person Controler walk 0 Answers
Excluding Gameobjects/Colliders from reacting with Collision Flags? 1 Answer