Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Resolved After asigning a transform to look at a raycast hit point makes it woble even tho the inspector says

Discussion in 'Scripting' started by stakensj, Jan 13, 2022.

  1. stakensj

    stakensj

    Joined:
    Oct 23, 2020
    Posts:
    25
    Hello! So I have made a script, that shoots a ray cast from the player's camera and then rotates the assigned transform to look at it. But after looking for a few seconds, the game object starts to wobble.

    Here is the issue:


    And here is the script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Raycaster : MonoBehaviour
    6. {
    7.  
    8.     public float damage = 10f;
    9.     public float range = 100f;
    10.     public float fireRate = 15f;
    11.     public float impactForce = 30f;
    12.  
    13.     public Camera fpscamera;
    14.     public Transform WhatLooks;
    15.     public Transform WhatLooks2;
    16.  
    17.     private float nextTimeToFire = 0f;
    18.  
    19.    
    20.     void Update()
    21.     {
    22.  
    23.      
    24.  
    25.             Shoot();
    26.        
    27.  
    28.     }
    29.  
    30.     void Shoot()
    31.     {
    32.  
    33.  
    34.         RaycastHit hit;
    35.         if (Physics.Raycast(fpscamera.transform.position, fpscamera.transform.forward, out hit, range))
    36.         {
    37.             UnityEngine.Debug.Log(hit.point );
    38.             WhatLooks.transform.LookAt(hit.point);
    39.             WhatLooks2.transform.LookAt(hit.point);
    40.         }
    41.  
    42.     }
    43. }
    44.  
     
  2. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,492
    Have you tried doing it in the LateUpdate instead of Update
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,685
    First, figure out if it's a raycasting problem or a rotation problem. Put this after the raycast line:
    Code (csharp):
    1. Debug.DrawLine(fpscamera.transform.position, hit.point);
    And watch where it draws the line to in the scene view. Does the line also jitter? Or is it smooth? If the line jitters, then try to find a pattern in how it jitters - maybe it's hitting an unexpected invisible collider somewhere, for example.

    If the line is smooth, then the problem is with rotating them somehow - either they're rotating wrong in the first place, or something else is affecting them after you rotate them. Try putting this after your LookAt's:
    Code (csharp):
    1. Debug.DrawLine(WhatLooks.transform.position, WhatLooks.transform.forward);
    Now does this line point in the direction you expect even after the objects start to jitter? If so, then something else is trying to rotate the guns - look around for physics or other scripts.
     
  4. stakensj

    stakensj

    Joined:
    Oct 23, 2020
    Posts:
    25
    Thank you both @StarManta and @SpookyCat for answering, I tried debugging it as @StarManta said, and apparently, it was the problem of the raycast, but then I tried using the LateUpdate function like @SpookyCat and now it does not jitter. My problem is now fixed. Thank you!
    Best Regards, stakensj!
     
unityunity