- Home /
Slow down acceleration
Hello Unity Community,
I have a script that has an object accelerate but my problem is that it accelerates to fast. How can I slow down the acceleration? I figured I could do something with the modulus operator but I can't seem to figure it out. Here's my code:
using UnityEngine;
using System.Collections;
public class MoveForward : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public int i;
public int MaxSpeed = 500;
public int MinSpeed = 5;
// Update is called once per frame
void FixedUpdate () {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), (2*i)/100);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
if (i < MaxSpeed){
i++;
print (i);
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
The i variable is what's making the object accelerate. The i with the if statement is where I need to put my code for slowing down acceleration. Any help by you fine folks is appreciated. :-)
Answer by lil_billy · Nov 17, 2012 at 08:33 PM
um no its not i is doing nothing the only thing affecting your speed / acceleration is
speed
which you can change
now I dont know what the physics looks like on your side but if you say it is accelerating then you can just change speed
if it isnt accelerating I recommend looking into MathF.Lerp and lerping the speed
edit: maybe I should explain the script to you
charactercontroller.move is what makes your character move movedirection is the math data that is passed to it to move it by whatever you want as far as i can see i never influences movedirection or any of the variables that influence moveDirection
Did you miss this line?: moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), (2*i)/100);
I've experimented with this script and when I take out the variable i altogether the object stops accelerating and ins$$anonymous$$d the object moves at a constant speed. So, i is affecting acceleration. I'll look into $$anonymous$$athF.Lerp. Does anyone have any other suggestions?
yes i did miss that bit, it was hidden by the scroll bar
so how about this i+=.5 or .25
and with deceleration -=.25
and change i to a float :D
oh and you dont want to run all of this in fixed update ins$$anonymous$$d try to do all of this in update and if and only if it jitters then put the final move task into fixed update
Your answer
Follow this Question
Related Questions
Rotational Acceleration to a Maximum Speed 1 Answer
Gliding vs bad acceleration dilema 0 Answers
Move Cube With Force and slow it down 1 Answer
Object move using android Accelerameter. 1 Answer
What values does acceleration have? 0 Answers