- Home /
Enemies vibrate along Z axis
I'm making a 3D game with the camera set to orthographic to keep movements and boundaries simple.
When my player moves (using AddForce), it appears to move much slower when moving vertically (Z axis) than horizontally (X axis), because of the camera angle. I fixed this by adding a vertEnhancer variable to multiply in the AddForce equation. It works pretty well, multiplying all vertical movements by 2.
I just learned how to move enemies toward the player by using "enemyRB.AddForce((playerPos - enemyPos) * enemySpeed);". This works, but I'm left with the slower vertical movements unless I can apply the same vertEnhancer to ONLY the Z movement, but that's not possible without atomizing the formula.
So I did. I spent like two days figuring out how to parse (playerPos - enemyPos) into individual coordinates, and ended up with what you see below. Now it works almost perfectly!! EXCEPT--when it moves in a pure vertical direction, it shakes, ever so slightly, but it's weird. And I really don't even know where to start to fix that. It doesn't happen when they move diagonally, and it does happen whether or not I include the vertEnhancer.
Any ideas??
:
:
PLAYER MOVEMENT (WORKS GREAT):
void Update()
{
//Movement
float horizontalInput = Input.GetAxisRaw("Horizontal");
float verticalInput = Input.GetAxisRaw("Vertical");
float vertEnhancer = 2;
playerRB.AddForce(Vector3.forward * playerSpeed * vertEnhancer * verticalInput, ForceMode.Impulse);
playerRB.AddForce(Vector3.right * playerSpeed * horizontalInput, ForceMode.Impulse);
ENEMY MOVEMENT (AS THE TUTORIAL TAUGHT):
void Update()
{
//Movement
Vector3 enemyVector = (player.transform.position - transform.position).normalized;
enemyRB.AddForce(enemyVector * enemySpeed);
}
ENEMY MOVEMENT (ATOMIZED FOR APPLYING vertEnhancer):
void Update()
{
//Movement
float playerPosX = player.transform.position.x;
float playerPosZ = player.transform.position.z;
float enemyPosX = transform.position.x;
float enemyPosZ = transform.position.z;
float vertEnhancer = 2;
Vector3 enemyVectorVert = new Vector3(0, 0, playerPosZ) - new Vector3(0, 0, enemyPosZ);
Vector3 enemyVectorHori = new Vector3(playerPosX, 0, 0) - new Vector3(enemyPosX, 0, 0);
enemyRB.AddForce(enemyVectorVert.normalized * enemySpeed * vertEnhancer, ForceMode.Impulse);
enemyRB.AddForce(enemyVectorHori.normalized * enemySpeed, ForceMode.Impulse);
Your answer
Follow this Question
Related Questions
AddForce/ForceMode.Impulse 1 Answer
Rigidbody2D AddForce only working with large numbers 2 Answers
RigidBody.AddForce not working 1 Answer
Limit object position which is moved by accelerometer 0 Answers
Smooth motion physics Playmaker 0 Answers