How can I make character accelerate and deccelerate using GetAxis?
I want to make my own character to controller. Sure the default is great BUT I want to learn how to do my own and understand it by doing. My question is how to make a character accelerate and deccelerate using get axis. I can manage doing it using OnGetKey(Blah) but on GetAxis is a blur. Not really sure how it works on inputs. So How do I refer to it when a player press a key, and how to do I refer to it when a player lets go of a key. Along that is managing speed in between all that.
Here is what I managed to do but I do not know where to go from there. Read on forums about mathf.clamp and smoothDamp but I need a solid understanding of the GetAxis use and how to go about it. I will provide code below so you can see what Im trying to do with it. This code accelerates properly but does so without waiting for input. So it needs to work on input and likewise backwards for decceleration. Thanks Everyone!
FYI: I will probably ask this later unless someone viewing may know the answer, how to tackle slopes once I do this.
{
private float xMove;
private float zMove;
public float speed;
public float currentSpeed = 0;
public float acc;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
movementInput();
}
void movementInput()
{
if (currentSpeed < speed)
{
currentSpeed += acc * Time.deltaTime;
}
xMove = Input.GetAxis("Horizontal") * currentSpeed * Time.deltaTime;
zMove = Input.GetAxis("Vertical") * currentSpeed * Time.deltaTime;
transform.Translate(new Vector3(xMove, 0, zMove));
}
}
Ive made some progress and managed to decipher input.getaxis a little more. I also tested this with joysticks. So Here is my code to figure out whats doing what and some explanations of my findings. If this helped anyone and was able to proceed please let me know. I will help as I make more progress but if anyone makes progress quicker we can all solve this problem faster. THAN$$anonymous$$S!
private float x$$anonymous$$ove;
private float z$$anonymous$$ove;
public float speed;
// Update is called once per frame
void Update ()
{
movementInput();
}
void movementInput()
{
x$$anonymous$$ove = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime; //Inputs for up and down
z$$anonymous$$ove = Input.GetAxisRaw("Vertical") * speed * Time.deltaTime; //Inputs for left and right
transform.Translate(new Vector3(x$$anonymous$$ove, 0, z$$anonymous$$ove)); //move my object upon input
//Input.GetAxis are Values between 1- and 1.
//Vertical means 1 is up and -1 is down 0 is no action. It also takes joystic zones into account. IE: half way up on joystic is 0.5
//Input.GetAxis here uses > or < to take into account slight movement of Joystick. Other wise using == 1/ == -1 / == 0 would ignore slight joystick movement
//NOTE:Input.GetAxis VS Input.GetAxisRaw
//Input.GetAixs mimics joystick movements on keyboard and gradually works up to 1 or -1. Can cause issues creating acceleration and decceleration mimmicking slow joystick movement to full press with additional drag. It seems like drag but isn't.
//Input.GetAxisRaw immediately goes to desired speed
//NOTE: == 0 on both vertical and Horizontal execute at the same time. So leaving one would probably be best because I do not want any future parameters executing twice
//-------------VERTICAL $$anonymous$$OVE$$anonymous$$ENT-----------------
if (Input.GetAxisRaw("Vertical") > 0)
{
print("Up");
}
if (Input.GetAxisRaw("Vertical") < 0)
{
print("Down");
}
if (Input.GetAxis("Vertical") == 0)
{
//print("Unpressed Vertical");
}
//--------------HORIZONTAL $$anonymous$$OVE$$anonymous$$ENT---------------
if (Input.GetAxisRaw("Horizontal") > 0)
{
print("Right");
}
if (Input.GetAxisRaw("Horizontal") < 0)
{
print("Left");
}
if (Input.GetAxis("Horizontal") == 0)
{
//print("Unpressed Horizontal");
}
}
Your answer
Follow this Question
Related Questions
Touch Controls without virtual joystick 0 Answers
8-way 2D top down movement (diagonal) idle animation issues 0 Answers
Roll a Ball Tutorial issue 0 Answers
Character Cannot Move from its Spot 0 Answers