Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to get a object to rotate around another while still looking at a other

Discussion in 'Scripting' started by Roadkill3846298356923865, May 8, 2024.

  1. Roadkill3846298356923865

    Roadkill3846298356923865

    Joined:
    Apr 15, 2024
    Posts:
    6
    Im wanting my enemy's gun to rotate around the enemy but still focus on the player.
    I have already tried to add a offset to the gun but it just hangs out next to the enemy and rotates toward the player not rotating around the enemy to the player. There are no errors in the code but when it is set to the enemy position it messes with some of the hit boxes and I havent been able to find any tutorials online to help with this specific problem.


    Code (CSharp):
    1. public Transform player;
    2.     public Rigidbody2D rb;
    3.     public GameObject enemy;
    4.  
    5.     private Vector3 LookingRight;
    6.     private Vector3 LookingLeft;
    7.  
    8.    public float theAngle;
    9.  
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.    
    15.    
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     private void Update()
    20.     {
    21.      
    22.  
    23.         Vector3 direction = player.position - transform.position;
    24.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    25.         rb.rotation = angle;
    26.         theAngle = angle;
    27.  
    28.      
    29.  
    30.         transform.position = enemy.transform.position;
    31.  
    32.      
    33.    
    34.     }
    35.  
    36.  
    37. }
     
    Last edited: May 8, 2024
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,015
    Cool! Let us know how it's going.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?


    To gain this information, I suggest debugging.

    By debugging you can find out exactly what your program is doing so you can fix it.

    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    Use the above techniques to get the information you need in order to reason about what the problem is.

    You can also use
    Debug.Log(...);
    statements to find out if any of your code is even running. Don't assume it is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.
     
  3. Roadkill3846298356923865

    Roadkill3846298356923865

    Joined:
    Apr 15, 2024
    Posts:
    6
    ok thank you I will edit the post so it shows more information
     
  4. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,934
    What is this script supposed to be attached to? The enemy or the gun?
    Just like your last thread, it is hard to know what it is you're trying to do.

    If the script is attached to the enemy, then
    transform.position = enemy.transform.position;
    doesn't do anything.

    If the script is attached to the gun, then why are you controlling the enemy RigidBody in the gun code?

    All you've done here is have "something" with a RigidBody rotate to possibly face the player. Who knows?

    You need a Player, Enemy, and Gun class.
     
    Last edited: May 9, 2024
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    When moving a rigidbody you should do it from FixedUpdate. If you don't want the gun to be knocked around by other rigidbodies then you need to set its Body Type to Kinematic. To move a Kinematic rigidbody around you should use MovePosition and MoveRotation because these functions result in interpolated/smooth movement.

    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         //rb.MoveRotation(Quaternion.LookRotation(Vector3.forward,player.position-transform.position)); // point our up vector towards the player
    4.         rb.MoveRotation(Quaternion.AngleAxis(Vector2.SignedAngle(Vector3.right,player.position-transform.position),Vector3.forward)); // point our right vector towards the player
    5.         rb.MovePosition(enemy.position+Quaternion.Euler(0,0,theAngle)*Vector3.right*4); // rotate around the enemy at a distance of 4 units
    6.         theAngle+=1;
    7.     }
     
  6. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    336
    You can achieve that in many ways.

    With transform, for example

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RotateAround : MonoBehaviour
    4. {
    5.     public Transform reference;
    6.     public Transform target;
    7.     public float length = 1f;
    8.     public float secondsPerCicle = 1f;
    9.  
    10.     public Quaternion localRotation;
    11.     private void Awake()
    12.     {
    13.         localRotation = target.rotation;
    14.     }
    15.  
    16.     public void Update()
    17.     {
    18.  
    19.         Quaternion rotationAround = Quaternion.AngleAxis((Time.time*360f)/secondsPerCicle, Vector3.up);
    20.         Vector3 localTranslation = rotationAround * (length * Vector3.right);
    21.         target.position = reference.localToWorldMatrix.MultiplyPoint3x4(localTranslation);
    22.  
    23. //Modify if the rotation is locked to a different reference
    24.         Quaternion rotation = Quaternion.LookRotation(target.position - reference.position, reference.up);
    25.         target.rotation = rotation*localRotation;
    26.  
    27.     }
    28. }
    29.  
    rotateAround.gif

    I see that you're using rigid bodies, in that case you have to do it through forces, but I encourages you to use transforms if rb is not strictly necessary in this functionality
     
  7. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    Maeslezo, there's already a method for rotating an object around another object while continuing to look at it:
    Code (CSharp):
    1. transform.RotateAround(target.position,Vector3.up,5*Time.deltaTime);
    Although it's not quite so useful for OP as I believe he wants to rotate a rigidbody gun around an object while looking at another object.
     
  8. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    336
    Indeed, you're totally right, I totally forgot it ‍:confused:

    Well, at least, he can modify my snippet to lock the rotation to anything else different to the origin :)
     
  9. Roadkill3846298356923865

    Roadkill3846298356923865

    Joined:
    Apr 15, 2024
    Posts:
    6


    Thank you so much this was just what I was looking for but Im just beginning coding and i cant figure out why the gun is speeding up and freaking out as I get closer It looks like its becoming two.
     
  10. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    336
    rotateAround_2.gif

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RotateAround : MonoBehaviour
    4. {
    5.     public Transform reference;
    6.     public Transform target;
    7.     public Transform lockRotation;
    8.    
    9.     public float length = 1f;
    10.     public float secondsPerCicle = 1f;
    11.  
    12.     public Quaternion localRotation;
    13.     private void Awake()
    14.     {
    15.         localRotation = target.rotation;
    16.     }
    17.  
    18.     public void Update()
    19.     {
    20.  
    21.         Quaternion rotationAround = Quaternion.AngleAxis((Time.time*360f)/secondsPerCicle, Vector3.up);
    22.         Vector3 localTranslation = rotationAround * (length * Vector3.right);
    23.         target.position = reference.localToWorldMatrix.MultiplyPoint3x4(localTranslation);
    24.  
    25.         Quaternion rotation = Quaternion.LookRotation(target.position - lockRotation.position, reference.up);
    26.         target.rotation = rotation*localRotation;
    27.  
    28.     }
    29. }
    30.  
     
  11. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    Yeah it's funny because for some reason it seems be a commonly misunderstood or forgotten function. It took me a while before I realized that it rotated an object around its own axis and not just around a pivot point. :)