Answer the question
In order to leave comments, you need to log in
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);
}
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;
.......
}
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"? Answer the question
In order to leave comments, you need to log in
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))
I copied the W key code
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); //перемещаем игрока в новую позицию
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question