B
B
BadCats2016-12-03 16:37:04
C++ / C#
BadCats, 2016-12-03 16:37:04

Coordinate transformation (function graph transformation) for character movement in unity?

Hello everyone, here is the key code D , which implements the movement of the character to the side with grief-in-half (more precisely, it turned out diagonally, but not the point)

if (Input.GetKey(KeyCode.D))
        {
PosZ = PosZ + speed+PosZ* Time.deltaTime;
            
            Newpos = PosZ;
            Vector3 directionD = new Vector3(Newpos*-1, hero.transform.position.y*-gravity, hero.transform.position.z);
            hero.Move(directionD*Time.deltaTime);
            lastPressedDTimeD = Time.time;
            if (Time.time - lastPressedDTimeA < threshold)
            {
                PosZ = PosZ + PosZ * -1;
            }
        }

But the key code A does not work correctly - the character moves in the wrong direction
if (Input.GetKey(KeyCode.A))
        {
            PosZ = PosZ + speed+PosZ * Time.deltaTime;//к текущей позиции добавили в направлении вперед немного)) 
            Newpos = PosZ;
            Vector3 directionA = new Vector3(Mathf.Sqrt(Newpos), hero.transform.position.y * -gravity,hero.transform.position.z);/*= new Vector3((Newpos * -1) * Mathf.Sqrt(Newpos * -1)*/
            lastPressedDTimeA = Time.time;


            hero.Move(directionA * Time.deltaTime);

            if (Time.time - lastPressedDTimeD < threshold)
            {
                PosX = PosX + PosX * -1;
            }

This is my reasoning on this
5822d4be77b74e09ba9ff9d2bcb03a89.JPG
(blue arrow - movement when pressing the A key)
Step No. 2 is based on the transformation
2ade1eab17a7415987e176a7f5517979.JPG
, this is exactly what I am trying to implement with this key, but for some reason it does not work out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TheTalion, 2016-12-03
@BadCats

Look up YouTube tutorials on how to make characters move. This is one of the easiest tasks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question