Too subjective and argumentative
How to move character controller on x, z, AND y axes?
I would like my character controller to be able to move on the x, y and z axes on key commands.
For example,
move on z axis with up and down arrows
move on x axis with left and right arrows
move on y axis with r key and f key
Of course moving on the x and z axis is the default and is working fine. But how do I add the ability to move on the y axis?
I've tried modifying FPSInputController script
from
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
to
var directionVector = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("UpDown"), Input.GetAxis("Vertical"));
And in ProjectSettings/Input, I assign
Horizontal to X axis and up down arrow keys and
Vertical to X axis and right left arrow keys and
I create a new axis called UpDown and assign it to the Y axis and r and f keys.
But no change happens. All the arrow keys act normally but r and f keys do nothing.
Any suggestions on modifying my approach? Or a whole new approach?
My game is a big object floating in space (no floor) and I want the character to be able to explore around the outside of it in all directions including up and down. (no gravity)
Thank you for any tips!
try applying the vertical one in the Y axis, don't know why you want to walk in all 3 axis though.
Hi. This question is off-topic, simply because it is a subjective, non-specific How-To question and it starts discussions. This doesn't mean it's a bad question though ;)
But, these kind of questions are best searched for on Google and or directly on Unity Forums. You may find hundreds of answers for "how to make a player move", because it's one of the most basic things. :)
Please stick to the forum rules when posting questions. You can find them here and here. It is also the moderator's fault for publishing this.
Another tip: When you accept an answer, it means the question is answered to your satisfaction and you should close the question now (using the small black cog wheel on the right of the question title). This helps keeping the forum organized and takes some work away from the moderators. :D
Answer by Masterio · Jan 07, 2016 at 10:37 AM
Just configure a rigidbody as you want.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
public class SpaceController : MonoBehaviour
{
public float speed = 1f;
private Rigidbody _rigidbody = null;
private float _y = 0f;
void Start()
{
_rigidbody = GetComponent<Rigidbody>();
}
void Update()
{
// move up
if(Input.GetKey(KeyCode.R))
{
_y = 1f;
}
// move down
else if(Input.GetKey(KeyCode.F))
{
_y = -1f;
}
else
{
_y = 0f;
}
// x, y, z move
Vector3 directionVector = new Vector3(Input.GetAxis("Horizontal"), _y, Input.GetAxis("Vertical"));
// add force to the rigidbody
_rigidbody.AddForce(directionVector * speed * Time.deltaTime);
}
}
I forgot about Time.deltaTime for speed :) Now it is updated.
You can thank people by upvoting their content.
Answer by alfonio · Jan 07, 2016 at 10:37 AM
using UnityEngine; using System.Collections;
public class Controls : MonoBehaviour {
public float PlayerSpeed = 5.0f;
//void start = first frame what to do
void Start () {
}
// Update is called once per frame
void Update () {
//movement
transform.Translate (Vector3.up * Input ("jump") * PlayerSpeed * Time.deltaTime);
transform.Translate (Vector2.right * Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime);//1.0.0
transform.Translate (Vector3.forward * Input.GetAxis("Vertical") * PlayerSpeed * Time.deltaTime);//0.0.1
}
}
this one works an is easy :D
You can thank people by upvoting their content.
Follow this Question
Related Questions
Character controller running stop animation transition 0 Answers
Huge drift Issue with xbox controllers. 0 Answers
Standard FPSController prefab ignores "Horizontal" and "Vertical" input on iOS 1 Answer
3rd person controller- anim parameters are not changing when i press stuff 0 Answers
Problem with the Character Controller on the Y-Axis,Character Controller drifting in the Y Axis 0 Answers