- First, I get the Input from the Keyboard or the Mouse:
float direction = Input.GetAxis("Horizontal");
float direction = Input.GetAxis("Mouse X");
Both returns values between -1 and 1, depending on the direction. - I get the actual position of the object
Vector3 posAtual = transform.position; - Move it!
# Changing the X of the vetor
transform.position = new Vector3(posAtual.x + (velocity * mouseDir * Time.deltaTime), posAtual.y, posAtual.z);
# Adding a new vetor to the actual position vector
transform.position += new Vector3(velocity * direction * Time.deltaTime, 0, 0);
# Adding a vector by direction
transform.position += transform.right * velocity * direction * Time.deltaTime;
# Following the mouse
Vector3 temp = Input.mousePosition;
temp.z = 10;
transform.position = new Vector3(Camera.main.ScreenToWorldPoint(temp).x, posAtual.y, posAtual.z);
# Adding Force
rb.AddForce(transform.right * force * direction);
# Using the Tranform method "Transalte"
transform.Translate(velocity * direction * Time.deltaTime, 0, 0);
1 de mai. de 2017
Ways to move an object in 2D
I found some ways to move an GameObject in the 2D enviroment.
Assinar:
Postar comentários (Atom)
Nenhum comentário:
Postar um comentário