Search Unity

How to rotate GameObjects keeping relative positions to another GameObject are fixed

Discussion in 'Getting Started' started by NyanGoodWill, Dec 25, 2017.

  1. NyanGoodWill

    NyanGoodWill

    Joined:
    Sep 6, 2017
    Posts:
    10
    I have a GameObject(Golfer) and Golfball GameObjects.

    I can spin the Golfer on swiping the screen.


    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    2.            {
    3.                f_difX = 0.0f;
    4.            }
    5.            else if (Input.GetMouseButton(0))
    6.            {
    7.                f_difX = Mathf.Abs(f_lastX - Input.GetAxis ("Mouse X"));
    8.  
    9.                if (f_lastX < Input.GetAxis ("Mouse X"))
    10.                {
    11.                    i_direction = -1;
    12.                    m_CurrentObj.transform.Rotate(Vector3.up, -f_difX);
    13.  
    14.                }
    15.  
    16.                if (f_lastX > Input.GetAxis ("Mouse X"))
    17.                {
    18.                    i_direction = 1;
    19.                    m_CurrentObj.transform.Rotate(Vector3.up, f_difX);
    20.  
    21.                }
    22.  
    23.                f_lastX = -Input.GetAxis ("Mouse X");
    24.            }

    With this code, the Golfer spin as shown in the following two images.

    https://stackoverflow.com/questions...ative-positions-to-another-gameobject-are-fix

    I need to rotate the Golfballs by keeping their relative positions to the Golfer are fixed.

    I think I need to use RotateAround.

    How can I rotated Golfballs with respect to the Golfer?