How can I make this character move smoothly?
so my script is:
public float speed = 50;
bool isinRL = true;
bool isinLL = false;
void Update ()
{
transform.Translate(0, 0, speed * Time.deltaTime);
if(isinRL && Input.GetKeyDown(KeyCode.A))
{
isinRL = false;
transform.Translate(-5, 0, 0);
}
if (isinLL && Input.GetKeyDown(KeyCode.D))
{
isinLL = false;
transform.Translate(5, 0, 0);
}
}
void IsInRL()
{
if(!isinRL)
{
isinRL = true;
isinLL = false;
}
}
void IsInLL()
{
if(!isinLL)
{
isinLL = true;
isinRL = false;
}
}
I need help finding out how I can make this part:
if(isinRL && Input.GetKeyDown(KeyCode.A))
{
isinRL = false;
transform.Translate(-5, 0, 0);
}
if (isinLL && Input.GetKeyDown(KeyCode.D))
{
isinLL = false;
transform.Translate(5, 0, 0);
}
}
when it says transform.translate, what I need help with is how do I move the character smoothly, because for me it just teleports there and anything else i try does not work, thanks!
Answer by Hellium · Oct 12, 2018 at 09:20 PM
I haven't tested the following code, but give it a try:
private float remainingXDistance = 0;
void Update ()
{
// Here, I suppose speed > 0
float absoluteDelta = speed * Time.deltaTime ;
float delta = absoluteDelta * Mathf.Sign( remainingXDistance ) ;
if( absoluteDelta > Mathf.Abs( remainingXDistance ) ) delta = remainingXDistance ;
remainingXDistance -= delta ;
transform.Translate(delta, 0, absoluteDelta );
if(isinRL && Input.GetKeyDown(KeyCode.A))
{
isinRL = false;
remainingXDistance -= 5 ;
}
if (isinLL && Input.GetKeyDown(KeyCode.D))
{
isinLL = false;
remainingXDistance += 5 ;
}
}
actually he moves, but he still teleports, and i cant have anything change the on the z axis becauser the character is already moving in that direction;
I made some mistakes because I was writing with a simple notepad, but the following script works perfectly fine. It seems you forgot to set isinLL
and isinRL
to true
in your conditions
public float speed = 1;
private float remainingXDistance = 0;
bool isinRL = true;
bool isinLL = false;
void Update ()
{
// Here, I suppose speed > 0
float absoluteDelta = speed * Time.deltaTime ;
float delta = absoluteDelta * $$anonymous$$athf.Sign( remainingXDistance ) ;
if( absoluteDelta > $$anonymous$$athf.Abs( remainingXDistance ) ) delta = remainingXDistance ;
remainingXDistance -= delta ;
transform.Translate(delta, 0, absoluteDelta );
if( isinRL && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
{
isinRL = false;
isinLL = true;
remainingXDistance -= 5 ;
}
if (isinLL && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))
{
isinRL = true;
isinLL = false;
remainingXDistance += 5 ;
}
}
the remainingXDistance for some reason it is not the number it is supposed to move, here is a video you can watch of it:
https://drive.google.com/open?id=1GRax$$anonymous$$ecIr_goSecv1TlvufJw8PUzvu33
Your answer
Follow this Question
Related Questions
C# I need to move object in a cycle 0 Answers
Navigation in VR Mode?!! 0 Answers
Unity3d Character Spawn Point 0 Answers
Swipe control smoothness problem 0 Answers