- Home /
Enemys jittering when moving
Hey guys, my game is not a rigidbody based game, so when I set the camera movement and the player movement to FixedUpdate, the player goes perfectly smooth, but the enemys now stutter. Does someone know how i can fix that. (It'a a 3D game)
The cameraMotor script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraMotor : MonoBehaviour { public Transform LookAt;
public float boundX = 0.5f; public float boundY = 0.5f; public float boundZ = 0.5f; public float speed = 0.15f;
private Vector3 desiredPosition;
// Update is called once per frame
void FixedUpdate()
{
Vector3 delta = Vector3.zero;
float dx = LookAt.position.x - transform.position.x;
// X Axis
if (dx > boundX || dx < -boundX)
{
if (transform.position.x < LookAt.position.x)
{
delta.x = dx - boundX;
}
else
{
delta.x = dx + boundX;
}
}
float dy = LookAt.position.y - transform.position.y;
// Y Axis
if (dy > boundX || dy < -boundY)
{
if (transform.position.y < LookAt.position.y)
{
delta.y = dy - boundY;
}
else
{
delta.y = dy + boundY;
}
}
float dz = LookAt.position.z - transform.position.z;
// Z Axis
if (dz > boundZ || dz < -boundZ)
{
if (transform.position.z < LookAt.position.z)
{
delta.z = dz - boundZ;
}
else
{
delta.z = dz + boundZ;
}
}
// Move the camera
desiredPosition = transform.position + delta;
transform.position = Vector3.Lerp (transform.position, desiredPosition, speed);
}
}
the playerMovement script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour { //Variables public CharacterController controller; public float gravity = -9.81f; public Transform groundCheck; public float groundDistance = 0.4f; public LayerMask groundMask; public float jumpHeight = 3f; public float speed;
Vector3 velocity; bool isGrounded; private bool canDoubleJump;
void FixedUpdate() {
//Gravity & GroundCheck
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -4f;
}
//Movement
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
velocity.y += gravity * Time.deltaTime;
controller.Move(move * speed * Time.deltaTime);
controller.Move(velocity * Time.deltaTime);
//Jump / DoubleJump
if(isGrounded)
{
canDoubleJump = true;
}
if(Input.GetButtonDown("Jump"))
{
if (isGrounded) {
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
} else {
if (canDoubleJump == true) {
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
canDoubleJump = false;
}
}
}
} }
The enemy AI script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; using EZCameraShake;
public class EnemyAI : MonoBehaviour { public NavMeshAgent agent; public Transform player; public LayerMask whatIsGround, whatIsPlayer; public float damage = 10f; public float range = 10000f;
public float magnitude;
public float roughness;
public float fadeInTime;
public float fadeOutTime;
public GameObject firingpoint;
bool alreadyAttacked;
//Patroling
public Vector3 walkPoint;
bool walkPointSet;
public float walkPointRange;
//Attacking
public float timeBetweenAttacks;
//States
public float sightRange, attackRange;
public bool playerInSightRange, playerInAttackRange;
private void Update()
{
//Check for sight and attack range
playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
if (!playerInSightRange && !playerInAttackRange) Patroling();
if (playerInSightRange && !playerInAttackRange) ChasePlayer();
if (playerInAttackRange && playerInSightRange) AttackPlayer();
if (playerInSightRange){
Vector3 playerPosition = new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z);
transform.LookAt(playerPosition);
}
}
private void Patroling()
{
if (!walkPointSet) SearchWalkPoint();
if (walkPointSet)
agent.SetDestination(walkPoint);
Vector3 distanceToWalkPoint = transform.position - walkPoint;
//Walkpoint reached
if (distanceToWalkPoint.magnitude < 1f)
walkPointSet = false;
}
private void SearchWalkPoint()
{
//Calculate random point in range
float randomZ = Random.Range(-walkPointRange, walkPointRange);
float randomX = Random.Range(-walkPointRange, walkPointRange);
walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
walkPointSet = true;
}
private void ChasePlayer()
{
transform.LookAt(player);
transform.Translate(Vector3.forward*7*Time.deltaTime);
}
private void AttackPlayer()
{
if (!alreadyAttacked)
{
Shoot();
alreadyAttacked = true;
Invoke(nameof(ResetAttack), timeBetweenAttacks);
}
}
private void ResetAttack()
{
alreadyAttacked = false;
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(firingpoint.transform.position, firingpoint.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
DamageObject target = hit.transform.GetComponent<DamageObject>();
if (target != null)
{
target.TakeDamage(int damage);
CameraShaker.Instance.ShakeOnce(magnitude, roughness, fadeInTime, fadeOutTime);
}
}
}
public void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, attackRange);
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, sightRange);
}
I really hope someone know how i can fix that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613054814im_/https://answers.unity.com/themes/thub/images/avi.jpg)