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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

RayCastHit is snapping (?)

Discussion in 'Scripting' started by Powerzaba, Aug 22, 2019.

  1. Powerzaba

    Powerzaba

    Joined:
    Dec 19, 2018
    Posts:
    8
    Hello! I've just recently started using Unity for fun, in order to learn the editor I've decided to work on a very simple top-down shooter, but I've encountered a problem, I'm trying to shoot a ray cast from my character (a rectangle) to the point it hits a wall. But it's snapping to specific hit points and I don't understand how to fix it.
    Here's a gif explaining the issue:


    Here's the code for the two scripts I'm using, one for movement and one for a potential shooting mechanic.
    Controller
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Controller : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     float speed = 0.6f;
    9.  
    10.     private Vector3 moveDirection = Vector3.zero;
    11.     private CharacterController characterController;
    12.     private float rayLenght;
    13.     public Vector3 pointToLookAt;
    14.  
    15.     void Start()
    16.     {
    17.         characterController = GetComponent<CharacterController>();
    18.        
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0.0f, Input.GetAxisRaw("Vertical")).normalized;
    24.         moveDirection *= speed;
    25.  
    26.         characterController.Move(moveDirection * Time.deltaTime);
    27.         Turning();
    28.     }
    29.  
    30.     void Turning()
    31.     {
    32.         Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
    33.         Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    34.         if (groundPlane.Raycast(cameraRay, out rayLenght))
    35.         {
    36.             pointToLookAt = cameraRay.GetPoint(rayLenght);
    37.             transform.LookAt(new Vector3(pointToLookAt.x, transform.position.y, pointToLookAt.z));
    38.         }
    39.     }
    40. }
    41.  
    Shooting
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Shooting : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     GameObject spawnPoint;
    9.  
    10.  
    11.     //private RaycastHit hit;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (Physics.Raycast(spawnPoint.transform.position, transform.forward * 100, out RaycastHit hit))
    23.         {
    24.             Debug.DrawRay(spawnPoint.transform.position, hit.transform.position, Color.blue);
    25.         }
    26.     }
    27.  
    28. }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    This
    Code (CSharp):
    1. Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    creates ray starting from camera to mouse pointer position.
     
  3. Powerzaba

    Powerzaba

    Joined:
    Dec 19, 2018
    Posts:
    8
    Hi! sorry, what do you mean? I'm using that to retrieve the point where the rectangle should be facing, it is rotating correctly, am I doing something wrong in how this should be done?
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    Ah now I see second script. You said you want to shot raycast from character, but shooting it from spawn point instead, why?
     
  6. Powerzaba

    Powerzaba

    Joined:
    Dec 19, 2018
    Posts:
    8
    So I would like to shoot a ray from the character or the spawn point, it doesn't really matter from where, since the two points move close together. The problem is that when i try to physics.raycast from either the character origin point or the spawn point (which is located just in front of the character) and try to log the position of where the ray hits a wall i only get 4 positions and you can see visually how the blue ray snaps on those locations.
     
  7. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    That's how you draw the ray. You do it wrong. If ray hits object, you draw blue line from ray origin to hit object transform. Of course you'll get only 4 positions since there are only 4 walls. Draw draw a line from origin to hit point instead.
     
  8. Powerzaba

    Powerzaba

    Joined:
    Dec 19, 2018
    Posts:
    8
    Code (CSharp):
    1.  if (Physics.Raycast(spawnPoint.transform.position, transform.forward * 100, out RaycastHit hit))
    2.         {
    3.             Debug.DrawRay(spawnPoint.transform.position, hit.transform.position, Color.blue);
    4.         }
    I'm shooting a ray from spawPoint (origin) in the forward direction, and if I hit something I draw a ray from spawPoint (origin) till the hit.transform.position, is this wrong?
     
  9. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    Yes. hit.transform.position gives you wall pivot, but you need the hit.point. This is exact point where raycast hit that wall.
     
  10. Powerzaba

    Powerzaba

    Joined:
    Dec 19, 2018
    Posts:
    8
    Ohhh this makes so much sense! I fixed it! Thank you very much for your patience and for your help!