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.

How can ı add a model to my hit point ?

Discussion in 'Physics' started by Lupinder, Nov 27, 2022.

  1. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    I want to add a model to my hit point after i clicked with my mouse to hitpoint. My project is about swinging so for better visual it would be nice to add a hook or something in end of the rope. Where should ı start it ?

    Code (CSharp):
    1. private void FixedUpdate()
    2.         {
    3.  
    4.             if (ProMouse.LeftButton.IsPressed || Input.GetKey(KeyCode.Q) && grapplingRope.Grappling)
    5.             {
    6.                _object.SetActive (true);
    7.  
    8.  
    9.                 grappleHolder.rotation = Quaternion.Slerp(grappleHolder.rotation, Quaternion.LookRotation(-(grappleHolder.position - _hit)), rotationSmooth * Time.fixedDeltaTime);
    10.  
    11.  
    12.                 var distance = Vector3.Distance(player.transform.position, _hit);
    13.                 if (!(distance >= minPhysicsDistance) || !(distance <= maxPhysicsDistance)) return;
    14.  
    15.                 if(timer >= 0.004006f) {
    16.                 timer = 0f;
    17.  
    18.                 player.playerRigidBody.velocity += pullForce * Time.fixedDeltaTime * yMultiplier * Mathf.Abs(_hit.y - player.transform.position.y) * (_hit - player.transform.position).normalized;
    19.                 player.playerRigidBody.velocity += pushForce * Time.fixedDeltaTime * player.transform.forward;
    20.  
    21.     }
    22.             }
    23.  
    24.             else
    25.             {
    26.                 grappleHolder.localRotation = Quaternion.Slerp(grappleHolder.localRotation, Quaternion.Euler(0, 0, 0), rotationSmooth * Time.fixedDeltaTime);
    27.             }
    28.  
    29.   private bool RaycastAll(out RaycastHit hit)
    30.         {
    31.             var divided = raycastRadius / 2f;
    32.             var possible = new NativeList<RaycastHit>(raycastCount * raycastCount, Allocator.Temp);
    33.             var cam = player.playerCamera.transform;
    34.  
    35.             for (var x = 0; x < raycastCount; x++)
    36.             {
    37.                 for (var y = 0; y < raycastCount; y++)
    38.                 {
    39.                     var pos = new Vector2(
    40.                         Mathf.Lerp(-divided, divided, x / (float)(raycastCount - 1)),
    41.                         Mathf.Lerp(-divided, divided, y / (float)(raycastCount - 1))
    42.                     );
    43.  
    44.                     if (!Physics.Raycast(cam.position + cam.right * pos.x + cam.up * pos.y, cam.forward, out var hitInfo, maxDistance)) continue;
    45.  
    46.                     var distance = Vector3.Distance(cam.position, hitInfo.point);
    47.                     if (hitInfo.transform.gameObject.layer != whatToGrapple) continue;
    48.                     if (distance < minDistance) continue;
    49.                     if (distance > maxDistance) continue;
    50.                     {
    51.  
    52.                     }
    53.                     possible.Add(hitInfo);
    54.                 }
    55.             }
    56.  
    57.             var arr = possible.ToArray();
    58.             possible.Dispose();
    59.  
    60.             if (arr.Length > 0)
    61.             {
    62.                 var closest = new RaycastHit();
    63.                 var distance = 0f;
    64.                 var set = false;
    65.  
    66.                 foreach (var hitInfo in arr)
    67.                 {
    68.                     var hitDistance = DistanceFromCenter(hitInfo.point);
    69.  
    70.                     if (!set)
    71.                     {
    72.                         set = true;
    73.                         distance = hitDistance;
    74.                         closest = hitInfo;
    75.                     }
    76.                     else if (hitDistance < distance)
    77.                     {
    78.                         distance = hitDistance;
    79.                         closest = hitInfo;
    80.                     }
    81.                 }
    82.  
    83.                 hit = closest;
    84.                 return true;
    85.             }
    86.  
    87.             hit = new RaycastHit();
    88.             return false;
    89.         }
    90.  
    91.         private float DistanceFromCenter(Vector3 point)
    92.         {
    93.             return Vector2.Distance(player.playerCamera.WorldToViewportPoint(point),
    94.                 new Vector2(0f, 0f));
    95.         }
    96.     }
    97. }
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,037
    I can't tell what the code you posted is actually supposed to do, but it looks like you already know the hit point from raycasting. Just set the hook's position to the hit point.
     
  3. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    Code (CSharp):
    1.  public GameObject hook;
    2.  
    3.  
    4. hook.setActive(false)
    I tried couple things but it did not work. I want to activate this game object at hitpoint after ı click to that point. How can ı connect codes ? Thanks for help
     
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,306
    How didn't it work? Did the hook not enable at all? Did it enable it was in the wrong position? How are you setting its position? How are you enabling it?

    You're showing us all the code except the code that we need to see.
     
  5. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    Code (CSharp):
    1.     private void FixedUpdate()
    2.         {
    3.  
    4.             if (ProMouse.LeftButton.IsPressed || Input.GetKey(KeyCode.Q) && grapplingRope.Grappling)
    5.             {
    6.                _object.SetActive (true);
    7.          hook.SetActive(true);
    8.  
    9.                 grappleHolder.rotation = Quaternion.Slerp(grappleHolder.rotation, Quaternion.LookRotation(-(grappleHolder.position - _hit)), rotationSmooth * Time.fixedDeltaTime);
    10.  
    11.          hook = Instantiate(hook, transform.position, Quaternion.FromToRotation(Vector3.up, hook)) as GameObject;
    12.  
    13.  
    14.                 var distance = Vector3.Distance(player.transform.position, _hit);
    15.                 if (!(distance >= minPhysicsDistance) || !(distance <= maxPhysicsDistance)) return;
    16.  
    17.                 if(timer >= 0.004006f) {
    18.                 timer = 0f;
    19.  
    20.                 player.playerRigidBody.velocity += pullForce * Time.fixedDeltaTime * yMultiplier * Mathf.Abs(_hit.y - player.transform.position.y) * (_hit - player.transform.position).normalized;
    21.                 player.playerRigidBody.velocity += pushForce * Time.fixedDeltaTime * player.transform.forward;
    22.  
    23.     }
    24.             }
    25.  
    26.             else
    27.             {
    28.                 grappleHolder.localRotation = Quaternion.Slerp(grappleHolder.localRotation, Quaternion.Euler(0, 0, 0), rotationSmooth * Time.fixedDeltaTime);
    29.             }
    30.     }
    31.  
    This is what i did but it did not work. "Argument 2: cannot convert from 'UnityEngine.GameObject' to 'UnityEngine.Vector3' " This is the error ı get. I think ı should do it differently but ı could not find solution.
     
  6. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,306
    How much of this code did you write and how much of it did you copy and past from other sources without learning how it works first? "hook" is a gameobject, not a vector3, and if you look at the documentation for the function you're using it in you'd see why that isn't working.

    https://docs.unity3d.com/ScriptReference/Quaternion.FromToRotation.html
     
  7. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    Okey but still how i will attach my game object to that hitpoint ? İf you give me a reference code, it will be more helpful. I looked documents, it is just did not work. Because of that i came here.
     
  8. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,306
    I explain the problem here and the link expands on it. You are trying to put a gameobject in FromToRotation when it needs a vector3. The error even says this. If you are having this much trouble, your problem is that you don't understand how instantiation works in the first place.

    https://learn.unity.com/tutorial/instantiate
     
  9. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    Thank you very much ı solved the problem.
     
  10. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    thanks for trying to help. I solved the problem.