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. Dismiss Notice

Question Rotate an axis to raycast hit

Discussion in 'Physics' started by liambilly, Jul 19, 2023.

  1. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    86
    hi, i have been trying to rote a parentedobject to ray cast hit , its not working correctly
    Code (CSharp):
    1. if (Physics.Raycast(UIManager.instance.cam.position, UIManager.instance.cam.forward, out hit, float.MaxValue, ~gameObject.layer))
    2.         {
    3.             Vector3 rotDir = hit.point - barell.position;
    4.  
    5.            
    6.  
    7.             var a = Quaternion.LookRotation(rotDir);
    8.             var rotgoal2 = Quaternion.RotateTowards(barell.localRotation, a, 10);
    9.             float rotgoal3 = Mathf.Clamp(rotgoal2.eulerAngles.x, 0, -45);
    10.             barell.localRotation = Quaternion.Euler(rotgoal3, 0, 0);
    11.            
    12.             print(rotgoal3);
    13.         }
     
  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    What do you mean "rotate to raycast hit"? Rotate around the axis of the ray? Look at the direction of the ray? What
     
  3. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    86
    make object lookat the direction of the ray but only rotate it on the x axis of the direction, the object is also parented
     
  4. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    Sorry it is still not understandable what you are trying to do. Rotate around the x axis of what? Whether its parented or not makes no difference if you use Transform.rotation instead of localRotation.
     
  5. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    It's better if you describe the context, what is this barell and what are you trying to do.
     
  6. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    86
    its a child of a swat tank,the barrel is the for shooting the ammo, so when i aim upward i want it to rotate on its x axis that is clamped so as to aim at the height of the hitpoint
     
  7. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    Ok then how do you acquire the target? If you are rotating the barrel up/down, you don't need a raycast, just map your rotation keys up/down to Transform.rotate your barrel around it's own local X axis, like

    barrelTransform.Rotate(1f, 0f, 0f);


    Is this good enough or do you actually need to raycast your target and do the "look at" with X axis only?
     
  8. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    86
    since I aim at the target with the camera, and not the barrell, i was preferin it to do a clamped rotation to the target
     
  9. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449