Trouble with button accelerate on face, simulate car
hi I'm trying to create a button when you press the button the sphere accelerate forward y keep going until the button is released, and keep going by the force and friction this sphere also move with the character controller using these references https://www.codegrepper.com/code-examples/csharp/unity+3d+character+controller
this what I have but don't work if (Input.GetButton("Fire1")) { m_Rigidbody.AddForce(transform.forward * friction); } thanks
also try with rigid body not working
void Update() { _isGrounded = Physics.CheckSphere(_groundChecker.position, GroundDistance, Ground, QueryTriggerInteraction.Ignore);
_inputs = Vector3.zero;
_inputs.x = Input.GetAxis("Horizontal");
_inputs.z = Input.GetAxis("Vertical");
if (_inputs != Vector3.zero)
transform.forward = _inputs;
if (Input.GetButtonDown("Jump") && _isGrounded)
{
_body.AddForce(Vector3.up * Mathf.Sqrt(JumpHeight * -2f * Physics.gravity.y), ForceMode.VelocityChange);
}
if (Input.GetButtonDown("Dash"))
{
Vector3 dashVelocity = Vector3.Scale(transform.forward, DashDistance * new Vector3((Mathf.Log(1f / (Time.deltaTime * _body.drag + 1)) / -Time.deltaTime), 0, (Mathf.Log(1f / (Time.deltaTime * _body.drag + 1)) / -Time.deltaTime)));
_body.AddForce(dashVelocity, ForceMode.VelocityChange);
}
}
Answer by omarsolarte · Jul 12, 2021 at 04:25 PM
i create some variable rate, velocity, and final velocity also decelerate value zo this is the code now works thanks to https://answers.unity.com/questions/514505/acceleration-script.html still trouble with decelerate but okk
if (Input.GetButton("Fire1"))
{
//add to the current velocity according while accelerating
currentVelocity = currentVelocity + (accelerationRate * Time.deltaTime);
transform.Translate(currentVelocity, 0, 0);
}
else
if(currentVelocity > 0) {
currentVelocity = currentVelocity - (decelerationRate * Time.deltaTime);
transform.Translate(currentVelocity, 0, 0);
}
else {
transform.Translate(0, 0, 0);
}
currentVelocity = Mathf.Clamp(currentVelocity, initialVelocity, finalVelocity);
//propel the object forward
transform.Translate(currentVelocity, 0, 0);
}