• 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 internallyx · Jun 07, 2020 at 06:11 AM · camerarotationshootingplayer movementshaking

Player jitters when moving.

Help!! So I have been suffering with t$$anonymous$$s for the past few days. I'm not sure if I should redo the code or what, but I have made so much progress so far so I'm uncertain about doing that. I don't want to wait to long to fix t$$anonymous$$s either

The goal is to have a player shoot w$$anonymous$$le facing the direction of a crosshair. The player itself is shooting though, not a pistol coming from a player. The problem is, the player shakes when it moves, even though it's able to shoot perfectly. I believe the problem is that the player rotates in one script file w$$anonymous$$le the player moves in the other. Let me show you:

Connected to the camera object:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PointAndShoot : MonoBehaviour
 {
     bool allowfire = true;
 
     public GameObject crosshairs, player, bulletPrefab, b;
 
     public float fireRate, bulletSpeed, currentAmmo, maxAmmo, AmountOnScreen, rotationZ;
 
     private Vector3 target;
     Vector3 difference;
 
     // Use t$$anonymous$$s for initialization
     void Start()
     {
         currentAmmo = maxAmmo;
         Cursor.visible = false;
     }
 
     // Update is called once per frame
     void Update()
     {
        if (player != null)
         {
             if (Cursor.visible == false)
             {
                 target = transform.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
                 crosshairs.transform.position = new Vector2(target.x, target.y);
                 difference = target - player.transform.position;
                 rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
                 player.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
             }
             if (Cursor.visible == true)
             {
                 target = new Vector3(0, 0, 0);
                 player.transform.rotation = Quaternion.identity;
             }
 
             if (Input.GetMouseButtonDown(0) && allowfire)
             {
                     float distance = difference.magnitude;
                     Vector2 direction = difference / distance;
                     direction.Normalize();
                     StartCoroutine(fireBullet(direction, rotationZ));
                     Debug.Log("Ammo: " + currentAmmo);
             }
         }
        
         IEnumerator fireBullet(Vector2 direction, float rotationZ)
         {
             if (currentAmmo > 0)
             {
                 allowfire = false;
                 b = Instantiate(bulletPrefab, player.transform.position, Quaternion.identity) as GameObject;
                 //b.transform.position = player.transform.position;
                 //b.transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
                 b.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
                 yield return new WaitForSeconds(fireRate);
                 currentAmmo--;
                 allowfire = true;
             }
         }
     }
 
    public float AmmoToGive(float ammoGiven)
     {
         if(currentAmmo < maxAmmo)
         {
             if(ammoGiven <= 0)
             {
                 return ammoGiven;
             }
             if(currentAmmo + ammoGiven <= maxAmmo)
             {
                 currentAmmo += ammoGiven;
                 return 0;
             }
             //get the difference between maxAmmo and currentAmmo
             float diff = maxAmmo - currentAmmo;
             //difference added to current ammo
             currentAmmo += diff;
             Debug.Log("Received " + diff + " bullets.");
             //return the ammo that wasn't used
             return (ammoGiven - diff);
         }
         return ammoGiven;
     }
 }

Connected to the Player:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerMovement : MonoBehaviour
 {
     //public Image healthBar;
     public int playerMaxHealth = 100;
     public int playerCurrentHealth;
     public int dmg;
 
     public float mSpeed;
     Rigidbody2D rbody;
 
     public Slider healthbar;
     public bool alive;
 
     // Start is called before the first frame update
     void Start()
     {
         playerCurrentHealth = playerMaxHealth;
         rbody = GetComponent<Rigidbody2D>();
         alive = true;
     }
 
     // Update is called once per frame
     void Update()
     {
         //movement
         float moveX = Input.GetAxis("Horizontal");
         float moveY = Input.GetAxis("Vertical");
     
         Vector2 move = new Vector2(moveX,moveY);
         rbody.velocity = move * mSpeed;
 
         healthbar.value = playerCurrentHealth;
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if (other.collider.CompareTag("Enemy"))
         {
             int health = GetDamaged(dmg);
             Debug.Log("Health is now:" + health);
             if (health <= 40 && health != 0) {
                 Debug.Log("Warning, critical damage!");
             }
             if (health <= 0) {
                 healthbar.value = 0;
                 Destroy(gameObject);
                 alive = false;
                 Time.timeScale = 0;
                 Debug.Log("Game over.");
             }
         }
     }
 
     public int GetDamaged(int attackdmg)
     {
         playerCurrentHealth -= attackdmg;
         return playerCurrentHealth;
     }
 }
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
Best Answer

Answer by muhammadtahiriqbal · Jun 07, 2020 at 07:25 AM

First Problem in your i found that is, you are applying velocity in Update it should be applied in FixedUpdate function are you sure about that is player jittering or camera

Comment
Add comment · Show 1 · 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
avatar image internallyx · Jun 07, 2020 at 08:20 AM 0
Share

That was the solution to my problem. I had to move the Quaternion.Euler upwards to FixedUpdate(). Then I had to reset interpolation to interpolation. It's moving well now. Thank you! :)

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

222 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 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

Cinemachine camera rotating with player 2D 1 Answer

Script that stays behind player that rotates the player along with the camera/mouse (third person) 0 Answers

Need help for player rotation and camera rotation 0 Answers

Camera Rotating when the player reach a specific position 0 Answers

How to rotate player rigidbody ? 1 Answer


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