B
B
BadCats2016-11-20 00:14:09
C++ / C#
BadCats, 2016-11-20 00:14:09

Character not moving?

There is a code:

if (Input.GetKey(KeyCode.D))
        {
            tempPos.z = tempPos.z * speed * Time.deltaTime;//изменяем позицию игрока
            Newpos = tempPos.z;
            hero.transform.position = new Vector3(tempPos.x,tempPos.y,Newpos);
               
        }

where:
public CharacterController hero;
    public float speed=2;
    Vector3 tempPos;

 // Use this for initialization
    void Start () {
        hero = gameObject.GetComponent<CharacterController>();
        

    }

    // Update is called once per frame
    void Update()
    {
        tempPos = hero.transform.position;//получаем позицию игрока
        float Newpos;//Поле, в которое попадут координаты преобразованные  в число типа float
        float CorrentPos=transform.position.x;
.......
}

That's the question, why when I press the D key, my character twitches for a fraction of a second - because at this time the
Z field of the transform component? which initially has a value of -1.02 i.e. this is the position of the character on the stage behaves as follows
  • -1.02
  • -4.603179e-18
  • -1.652926e-25
  • -7.104583e-43
  • 0
- a rather strange sequence, I can not identify any regularity. And the most interesting thing is that when it reaches zero, it stops, that is, it does not become negative.
And a similar method for the W key works fine
if (Input.GetKey(KeyCode.W))
        {
            tempPos.x = tempPos.x * speed * Time.deltaTime;//изменяем позицию игрока
            Newpos = tempPos.x;
            hero.transform.position =
                Vector3.MoveTowards(transform.position, //Переместить из текущего положения
                new Vector3(Newpos, hero.transform.position.y, hero.transform.position.z)/*нам нужно изменить только x координату*/ , speed);   //перемещаем игрока в новую позицию

        }
Maybe moving to the side has a completely different principle and I copied the code of the W key in vain in the hope of "correcting it a little"?
Maybe someone knows? Thanks in advance?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis Gaydak, 2016-11-20
@BadCats

Let me point out to you the main problem in the logic
tempPos.z = tempPos.z * speed * Time.deltaTime;//we change the player
's position in this line.
just substitute values. DeltaTime is essentially less than 1, and the speed is constant. The position of the character is also essentially a constant.
That is, in fact, if at the beginning the position of tempPos.z is different from zero, then you simply reduce it, tend to zero, taking the fractional part.
As an option, you need something like adding an offset to the current position
tempPos.z = tempPos.z + speed * Time.deltaTime * transform.forward; // add a little to the current position in the forward direction))

D
Dmitry, 2016-11-20
@Dimusikus

I copied the W key code

The keyword 'copied' does not look like a copy at all, at least the MoveTowards operator is missing in the code with all the ensuing circumstances.
Make a real copy-paste of the W key code (if it really works...) and replace the axes, and check.

B
BadCats, 2016-11-20
@BadCats

and this doesn't work either:

if(Input.GetKey(KeyCode.D))
        {
            tempPos.z = tempPos.z * speed * Time.deltaTime;//изменяем позицию игрока
            Newpos = tempPos.z;
            hero.transform.position =
                Vector3.MoveTowards(transform.position, //Переместить из текущего положения
                new Vector3(hero.transform.position.x,hero.transform.position.y,Newpos)/*нам нужно изменить только x координату*/ , speed);   //перемещаем игрока в новую позицию

        }

And the character moves forward - that is, the W key code is working

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question