Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
Virtual Accelerometer in Unity

I've seen many tutorials about how to use a smartphone's accelerometer as input to control an object in Unity, but is possible to simulate a tri-axial accelerometer in Unity itself?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can essentially get the "acceleration"(a.k.a the velocity) of an object by dividing the distance covered over time. In this case, that means breaking up a Vector3 into individual xyz variables, then calculating speed on each, and putting them back into a Vector3.

Code:

Vector3 lastPos;

void Start() {
  lastPos = transform.position;
}

void Update() {
  // define velocities as currentPos-lastPos(change) over time per frame, which gets velocity this frame... will be saved every frame
  float xVelocity = (transform.position.x-lastPos.x)/Time.deltaTime;
  float yVelocity = (transform.position.y-lastPos.y)/Time.deltaTime;
  float zVelocity = (transform.position.z-lastPos.z)/Time.deltaTime;
  
  // Do stuff with individual velocities(xyz)
  // ...
  // or store in Vector3
  Vector3 totalVelocity = new Vector3(xVelocity, yVelocity, zVelocity);
  // Do stuff with total velocity
  // ...

  // Set lastPos to transform.position (WARNING: Only do after you are done calculating velocity
  
  lastPos = transform.position;
}

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda