Hello,
sorry for the not easy question, but 'm having an hard time with an acceleration script.
My question, basically is almost identical to this:
http://answers.unity3d.com/questions/583863/move-player-via-button-press-with-including-accele.html
that’s not me though and it was left without answers :(. Also I use my own controls.
First, let me explain how my game works: basically you have a control stick on the left of your screen, but that’s ONLY to rotate the player.
I’m using ControlFreak’s sticks and buttons, and here’s the script:
var grades : float = 90;
var rotaz :float;
then, in a function Update, wrapped in the stick part:
rotaz = CFInput.GetAxis("Horizontal");
transform.Rotate(0, rotaz * grades * Time.deltaTime, 0);
this works perfectly so far, the player can turn around to decide the direction with the stick while he’s going.
To move the player I need a script that moves him when I press a touch button on release (game is for mobile).
Technically I already have the button ready:
movingZone.JustUniReleased())
{
var movingUniRelStartPos : Vector2 = movingZone.GetReleasedUniStartPos();
var movingUniRelEndPos : Vector2 = movingZoneGetReleasedUniEndPos();
var movingUniRelStartBox : int = TouchZone.GetBoxPortion(2, 2, movingZone.GetReleasedUniStartPos(TouchCoordSys.SCREEN_NORMALIZED));
var movingUniRelEndBox : int = TouchZone.GetBoxPortion(2, 2, movingZone.GetReleasedUniEndPos(TouchCoordSys.SCREEN_NORMALIZED));
var movingUniRelDragVel : Vector2 = movingZone.GetReleasedUniDragVel();
var movingUniRelDragVec : Vector2 = movingZone.GetReleasedUniDragVec();
//I probably need my script to be here
}
I had no problem accomplishing that at the beginning, as even a simple transform.Translate(Vector3.forward * speed) was sufficient.
The problem is that I need the player to accelerate AND decelerate but, like in that Unity Answers question it can’t be based on “how much the player holds down the button”.
It has to be that, the more you press (technically, release) the button, the more you keep accelerating, but in the moments you’re not pressing it it slowly decelerates. So if, say, you press it 20 times quickly and then stop, it still keeps going for a bit.
So far I’ve had two main problems:
The first is that my player, who’s also a rigidbody, does Not move only on the z axis so I guess I still have to use either Vector3.forward in some form or some rigidbody forces?
the second is that I’m not sure if I can use Time.deltaTime in my case, so I’m a bit lost here.
Thank you. If anyone can really help me and make this work, I’d make sure to give a small reward.