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

I need Help! Gun rotate around point

Discussion in 'Scripting' started by abdelrahmanyoussry509, May 2, 2020.

  1. abdelrahmanyoussry509

    abdelrahmanyoussry509

    Joined:
    Jan 26, 2020
    Posts:
    14
    hello i am new to unity: my problem is that i want when i grapple my gun rotates around the grapplepoint but it rotates the entire gun i want it to rotate the gun's y axis only

    weapon Sway code:
    problem is in line 38:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponSway : MonoBehaviour
    6. {
    7.     public float Intensity;
    8.     public float smooth;
    9.     private Quaternion OriginRotation;
    10.  
    11.  
    12.     private void Start()
    13.     {
    14.         OriginRotation = transform.localRotation;
    15.     }
    16.     private void Update()
    17.     {
    18.         UpdateSway();
    19.     }
    20.  
    21.     private void UpdateSway()
    22.     {
    23.         //controls
    24.         float t_X_mouse = Input.GetAxis("Mouse X");
    25.         float t_Y_mouse = Input.GetAxis("Mouse Y");
    26.  
    27.         //calcuate target rotation
    28.         Quaternion Target_adjX = Quaternion.AngleAxis(Intensity * t_X_mouse, Vector3.up);
    29.         Quaternion Target_adjY = Quaternion.AngleAxis(Intensity * t_Y_mouse, Vector3.right);
    30.         Quaternion TargetRotation = OriginRotation * Target_adjX * Target_adjY;
    31.  
    32.         //rotate towards target rotation
    33.  
    34.      
    35.            
    36.         if (GetComponent<GrapplingGunScript>().isGrapling)
    37.         {
    38.             transform.LookAt(GetComponent<GrapplingGunScript>().GrapplePoint);
    39.            
    40.         }
    41.         else if (!GetComponent<GrapplingGunScript>().isGrapling)
    42.         {
    43.             transform.localRotation = Quaternion.Lerp(transform.localRotation, TargetRotation, Time.deltaTime * smooth);
    44.         }
    45.  
    46.     }
    47. }
    48.  

    GrappleGun script:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7. public class GrapplingGunScript : MonoBehaviour
    8. {
    9.     private LineRenderer Lr;
    10.     public Vector3 GrapplePoint;
    11.     public LayerMask Graprable;
    12.     public Transform GunTip;
    13.     private float MaxDistance =100f;
    14.     private SpringJoint Joints;
    15.     public bool isGrapling = false;
    16.     private LayerMask LayerGrap;
    17.    
    18.  
    19.     private void Awake()
    20.     {
    21.         Lr = GetComponent<LineRenderer>();
    22.  
    23.     }
    24.     private void Start()
    25.     {
    26.         LayerGrap = LayerMask.NameToLayer("Grapelable");
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.         //rather than use (public transfrom) we used this becuse in a prefab we can not refrence them when we instaciate
    32.         //so we refrence them using tags
    33.         var cam = GameObject.FindWithTag("Cam");
    34.         var Player = GameObject.FindWithTag("Player");
    35.  
    36.  
    37.         if (Input.GetMouseButtonDown(0))
    38.         {
    39.             StartGrapple();
    40.            
    41.            
    42.         }else if (Input.GetMouseButtonUp(0))
    43.         {
    44.             StopGrapple();
    45.         }
    46.         void StartGrapple()
    47.         {
    48.             RaycastHit Hit;
    49.          
    50.             if (Physics.Raycast(origin: cam.transform.position,direction: cam.transform.forward,out Hit,MaxDistance,Graprable))
    51.             {
    52.                 if(Hit.transform.gameObject.layer == LayerGrap)
    53.                 {
    54.                     isGrapling = true;
    55.                     GrapplePoint = Hit.point;
    56.                     Joints = Player.gameObject.AddComponent<SpringJoint>();
    57.                     Joints.autoConfigureConnectedAnchor = false;
    58.                     Joints.connectedAnchor = GrapplePoint;
    59.  
    60.                     float DistanceFromPoint = Vector3.Distance(a: Player.transform.position, b: GrapplePoint);
    61.  
    62.                     //distance it will try to keep you from grappling
    63.                     Joints.maxDistance = DistanceFromPoint * 0.8f;
    64.                     Joints.minDistance = DistanceFromPoint * 0.25f;
    65.  
    66.                     //change values how you see fit
    67.                     Joints.spring = 4.5f;
    68.                     Joints.damper = 7f;
    69.                     Joints.massScale = 4.5f;
    70.  
    71.                     Lr.positionCount = 2;
    72.                 }
    73.                 else
    74.                 {
    75.                     return;
    76.                 }
    77.              
    78.             }
    79.         }
    80.      
    81.        
    82.     }
    83.  
    84.     private void LateUpdate()
    85.     {
    86.         DrawRope();
    87.     }
    88.     void DrawRope()
    89.     {
    90.         if (!Joints) return;
    91.        
    92.          
    93.        
    94.         Lr.SetPosition(index: 0, GunTip.position);
    95.         Lr.SetPosition(index: 1, GrapplePoint);
    96.     }
    97.    public void StopGrapple()
    98.     {
    99.         isGrapling = false;
    100.         Lr.positionCount = 0;
    101.         Destroy(Joints);
    102.     }
    103.     public bool IsGrappling()
    104.     {
    105.         return Joints != null;
    106.     }
    107.     public Vector3 GetGrapplePoint()
    108.     {
    109.         return GrapplePoint;
    110.     }
    111.  
    112. }
    113.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Get the GrapplePoint, store it in a Vector3, save that Vector3's Y component to your gun's Y position, then use that in your LookAt function.
     
  3. abdelrahmanyoussry509

    abdelrahmanyoussry509

    Joined:
    Jan 26, 2020
    Posts:
    14
    i am still confused
    Code (CSharp):
    1. GrapplePointer = GetComponent<GrapplingGunScript>().GrapplePoint;
    2.        
    3.  
    4.  
    5.         if (GetComponent<GrapplingGunScript>().isGrapling)
    6.         {
    7.             Vector3 gun = transform.localPosition;
    8.             gun.y = GrapplePointer.y;
    9.             transform.LookAt(gun);
    10.            
    11.         }
    12.         else if (!GetComponent<GrapplingGunScript>().isGrapling)
    13.         {
    14.             transform.localRotation = Quaternion.Lerp(transform.localRotation, TargetRotation, Time.deltaTime * smooth);
    15.         }
    can you give me an example? here the code i tried for what you toled me
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Oh, rereading mine, I typo'd "set" as "save", sorry. That would make it more confusing.

    Going back to the code you had before,
    Code (csharp):
    1.  
    2.             transform.LookAt(GetComponent<GrapplingGunScript>().GrapplePoint);
    3. //replace with
    4. Vector3 grapplePoint = transform.LookAt(GetComponent<GrapplingGunScript>().GrapplePoint);
    5. grapplePoint.y = transform.y;
    6. transform.LookAt(grappePoint);
     
  5. abdelrahmanyoussry509

    abdelrahmanyoussry509

    Joined:
    Jan 26, 2020
    Posts:
    14
    hey
    i dont want to be annoying (i am really sorry) but when i did exactly as you said and replaced it well i got 2 erros


    -Error CS0029 Cannot implicitly convert type 'void' to 'UnityEngine.Vector3'
    -Error CS1061 'Transform' does not contain a definition for 'y' and no accessible extension method 'y' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)

    (i know its annoying but would you please explain it to me?)
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Uh... yep, those are my fault. Typed that in half-asleep this morning.

    Code (csharp):
    1.  
    2. Vector3 grapplePoint = GetComponent<GrapplingGunScript>().GrapplePoint;
    3. grapplePoint.y = transform.position.y;
    4. transform.LookAt(grapplePoint);