Search Unity

Adding two rotations together

Discussion in 'Physics' started by Chimpulous, Jul 21, 2021.

  1. Chimpulous

    Chimpulous

    Joined:
    Feb 4, 2018
    Posts:
    10
    hello, im trying to create a building template that you can place with a mouse, but its a third person game view and i wanted the template to rotate around the player. I want right click to allow the template to be rotated as well but have the rotationstay relative to the camera view as it rotates around the player. The trouble is after I rotate the template the template doesn't stay in the rotated position. Its hard to explain but Ill add my code:

    Code (CSharp):
    1. if (Input.GetMouseButton(1))
    2.                     {
    3.                         rotationAmount += Input.GetAxis("Mouse X") * 10;
    4.  
    5.                         transform.GetChild(0).GetComponent<LookRotation>().freezeRotation = true;
    6.  
    7.                         Vector3 tempRot = new Vector3(0, lastRot.y + rotationAmount, 0);
    8.                         lastFreezeRot = newTemplate.transform.rotation.eulerAngles;
    9.  
    10.                         float lastFreezeTempY = lastFreezeRot.y;
    11.                         Debug.LogWarning("last freeze temp Y: " + lastFreezeTempY);
    12.  
    13.                         SetTemplatePositionServerRpc(lastPos, newTemplate.GetComponent<NetworkObject>().NetworkObjectId,tempRot);
    14.                         //Debug.LogWarning("last rotation: " + lastRotation.y);
    15.                     }
    16.                     else
    17.                     {
    18.                         transform.GetChild(0).GetComponent<LookRotation>().freezeRotation = false;
    19.  
    20.                         //this needs to get the player pos but subtract the temp rotation;
    21.                         GameObject localPlayer = NetworkSpawnManager.GetLocalPlayerObject().gameObject;
    22.  
    23.                         Vector3 playerRot = localPlayer.transform.rotation.eulerAngles;
    24.  
    25.  
    26.                         float lastFreezeTempY = lastFreezeRot.y;
    27.                         float playerRotY = playerRot.y;
    28.  
    29.                         float addedRotY = playerRotY - lastFreezeTempY;
    30.                         Debug.LogWarning("last freeze temp Y: " + lastFreezeTempY);
    31.                         Debug.LogWarning("player rot Y: " + playerRotY);
    32.                         Debug.LogWarning("added rot y: " + addedRotY);
    33.                         Vector3 playerPlusTempRot = new Vector3(0, addedRotY, 0);
    34.                         lastRot = playerPlusTempRot;
    35.                         SetTemplatePositionServerRpc(tempWorldPos,                       newTemplate.GetComponent<NetworkObject>().NetworkObjectId,playerPlusTempRot);
    36.                         lastPos = tempWorldPos;
    37.  
    38.                         rotationAmount = 0;
    39.                      
    40.                     }
    Can anyone help me out with this?
     
    Last edited: Jul 21, 2021