• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BadAtCS · Mar 30 at 10:58 AM · player movementenemy aicamera movementjitterjittering

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SuperCoderzzz · Mar 30 at 12:52 PM

IM DOCTOR MINECRAFT AND PROBABLY A CHILD PREDATOR

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mobile camera smooth 0 Answers

Third person camera gets closer to the player 0 Answers

Camera relative rigidbody third person player movement 1 Answer

Why incrementing transform.position causes jitter on iOS 1 Answer

Spherical Movement follow Camera jittery movement. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges