• 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 /
  • Help Room /
avatar image
0
Question by LabibM · Jun 05, 2017 at 10:31 PM · dynamiccrosshairshakey

[Solved, idk it fixed itself] Dynamic Crosshair is very jittery/shaky

Ok, so bascially I followed this guy's tutorial https://www.youtube.com/watch?v=rjXy8mmj-Kc&t=88s from the beginning and once i got to the crosshair one, i tried adjusting it and stuff but it just wouldnt stop jittering. alt text

Ok i reduced the code below to the things relating to the crosshair because i dont wanna post like 250 lines of code.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class UserInput : MonoBehaviour
 {
     public CharacterMovement characterMove { get; protected set; }
     public WeaponHandler weaponHandler { get; protected set; }
 
     [System.Serializable]
     public class OtherSettings
     {
         public float lookSpeed = 5.0f;
         public float lookDistance = 30.0f;
         public bool requireInputForTurn = true;
         public LayerMask aimDetectionLayers;
     }
     [SerializeField]
     public OtherSettings other;
 
     public Camera TPSCamera;
 
     Dictionary <Weapon, GameObject> crosshairPrefabMap = new Dictionary<Weapon, GameObject>();
   
     void SetupCrosshairs () {
         if (weaponHandler.weaponsList.Count > 0) {
             foreach (Weapon wep in weaponHandler.weaponsList) {
                 GameObject prefab = wep.weaponSettings.crosshairPrefab;
                 if (prefab != null) {
                     GameObject clone = (GameObject)Instantiate (prefab);
                     crosshairPrefabMap.Add (wep, clone);
                     ToggleCrosshair (false, wep);
                 }
             }
         }
     }
   
     //Handles all weapon logic
     void WeaponLogic()
     {
         if (!weaponHandler)
             return;
 
         aiming = Input.GetButton (input.aimButton) || debugAim;
         weaponHandler.Aim (aiming);
 
         if (Input.GetButtonDown (input.switchWeaponButton)) {
             weaponHandler.SwitchWeapons ();
             UpdateCrosshairs ();
         }
         
         if (weaponHandler.currentWeapon) {
             
             Ray aimRay = new Ray (TPSCamera.transform.position, TPSCamera.transform.forward);

             if (aiming) {
                 ToggleCrosshair (true, weaponHandler.currentWeapon);
                 PositionCrosshair (aimRay, weaponHandler.currentWeapon);
             }
             else
                 ToggleCrosshair (false, weaponHandler.currentWeapon);
         } else
             TurnOffAllCrosshairs ();
     }
 
     void TurnOffAllCrosshairs () {
         foreach (Weapon wep in crosshairPrefabMap.Keys) {
             ToggleCrosshair (false, wep);
         }
     }
 
     void CreateCrosshair (Weapon wep) {
         GameObject prefab = wep.weaponSettings.crosshairPrefab;
         if (prefab != null) {
             prefab = Instantiate (prefab);
             ToggleCrosshair (false, wep);
         }
     }
 
     void DeleteCrosshair (Weapon wep) {
         if (!crosshairPrefabMap.ContainsKey (wep))
             return;
 
         Destroy (crosshairPrefabMap [wep]);
         crosshairPrefabMap.Remove (wep);
     }
 
     // Position the crosshair to the point that we are aiming
     void PositionCrosshair (Ray ray, Weapon wep)
     {
         Weapon curWeapon = weaponHandler.currentWeapon;
         if (curWeapon == null)
             return;
         if (!crosshairPrefabMap.ContainsKey (wep))
             return;
 
         GameObject crosshairPrefab = crosshairPrefabMap [wep];
         RaycastHit hit;
         Transform bSpawn = curWeapon.weaponSettings.bulletSpawn;
         Vector3 bSpawnPoint = bSpawn.position;
         Vector3 dir = ray.GetPoint(curWeapon.weaponSettings.range) - bSpawnPoint;
 
         if (Physics.Raycast (bSpawnPoint, dir, out hit, curWeapon.weaponSettings.range, 
             curWeapon.weaponSettings.bulletLayers)) {
             if (crosshairPrefab != null) {
                 ToggleCrosshair (true, curWeapon);
                 crosshairPrefab.transform.position = hit.point;
                 crosshairPrefab.transform.LookAt (Camera.main.transform);
             }
         } else {
             ToggleCrosshair (false, curWeapon);
         }
     }
 
     // Toggle on and off the crosshair prefab
     void ToggleCrosshair(bool enabled, Weapon wep)
     {
         if (!crosshairPrefabMap.ContainsKey (wep))
             return;
 
         crosshairPrefabMap [wep].SetActive (enabled);
     }
 
     void UpdateCrosshairs () {
         if (weaponHandler.weaponsList.Count == 0)
             return;
 
         foreach (Weapon wep in weaponHandler.weaponsList) {
             if (wep != weaponHandler.currentWeapon) {
                 ToggleCrosshair (false, wep);
             }
         }
     }

This is my crosshair prefab As for the image inside, i just dragged my crosshair image onto it.

alt text

I can show you my project if needed. (This is for a school project so yeah) Ive gone through the tutorials like 4 times trying to see what i did wrong. I couldn't find it :\

18987482-10210884462800741-1587333587-o.png (430.1 kB)
18986251-10210884432359980-648257457-o.png (362.1 kB)
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 LabibM · Jun 05, 2017 at 10:54 PM

Uhhhh, i dont even know what i did but its fixed now. im confused. im still new to this forum so i dont know how to make this solved or stuff.

also if anyone could explain what might have happened, that would be cool

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

106 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

Related Questions

Scene name from additive async? 0 Answers

Help: Lighting a dynamic scene? 0 Answers

Customise the gazepointer of google cardboard unity 2 Answers

Best way to assign health according to level progress? 1 Answer

I have a list of GameObjects. They all have a script in common. Is there a way to load the script only once and control all the GameObjects in the list? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges