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

(Help) How To Rotate Object Based Off The Orientation Of The Main Camera

Discussion in 'Scripting' started by LemonMontage420, Jun 8, 2020.

  1. LemonMontage420

    LemonMontage420

    Joined:
    May 28, 2020
    Posts:
    56
    Here's My Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pickup : MonoBehaviour
    6. {
    7.     float throwForce = 600;
    8.     Vector3 objectPos;
    9.     float distance;
    10.     public float maxDistance = 3;
    11.  
    12.     public bool canHold = true;
    13.     public GameObject item;
    14.     public GameObject tempParent;
    15.     public bool isHolding = false;
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.      
    20.     {
    21.      
    22.  
    23.         distance = Vector3.Distance(item.transform.position, tempParent.transform.position);
    24.      
    25.         if (distance >= maxDistance)
    26.         {
    27.             isHolding = false;
    28.         }
    29.         if (isHolding == true)
    30.         {
    31.             item.GetComponent<Rigidbody>().velocity = Vector3.zero;
    32.             item.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
    33.             item.transform.SetParent(tempParent.transform);
    34.  
    35.             if (Input.GetMouseButtonDown(1))
    36.             {
    37.                 item.GetComponent<Rigidbody>().AddForce(tempParent.transform.forward * throwForce);
    38.                 isHolding = false;
    39.             }
    40.             if (Input.GetAxis("Mouse ScrollWheel") > 0)
    41.             {
    42.                 transform.Rotate(Vector3.left * 3f, Space.Self);
    43.             }
    44.             if (Input.GetAxis("Mouse ScrollWheel") < 0)
    45.             {
    46.                 transform.Rotate(Vector3.right * 3f, Space.Self);
    47.             }
    48.         }
    49.         else
    50.         {
    51.             objectPos = item.transform.position;
    52.             item.transform.SetParent(null);
    53.             item.GetComponent<Rigidbody>().useGravity = true;
    54.             item.transform.position = objectPos;
    55.         }
    56.      
    57.     }
    58.     void OnMouseOver()
    59.     {
    60.         if (distance <= maxDistance && Input.GetMouseButtonDown(0))
    61.         {
    62.  
    63.  
    64.         isHolding = true;
    65.         item.GetComponent<Rigidbody>().useGravity = false;
    66.         item.GetComponent<Rigidbody>().detectCollisions = true;
    67.  
    68.         }
    69.     }
    70.     void OnMouseUp()
    71.     {
    72.         isHolding = false;
    73.    
    74.     }
    75. }
    As the title says, I want the gameobject "item" to rotate from the main camera's orientation instead of it's own. (excuse my incompetence if the answer is really simple, this is my 1st week learning how to program in general).
     
  2. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    You could try saving the rotation the camera did in the frame and apply that rotation to your item? This will rotate your item by the same rotation the camera experiences. Is this what u wanted?
     
  3. LemonMontage420

    LemonMontage420

    Joined:
    May 28, 2020
    Posts:
    56
    This is an object that I want to pick up and rotate. My problem is that I want it to rotate based on the orientation of the player camera (main camera) so that no matter what side I rotate it from, it rotates in the same direction
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    take a directional vector from the camera, its forward or up vector for example, and rotate around that axis.
     
  5. LemonMontage420

    LemonMontage420

    Joined:
    May 28, 2020
    Posts:
    56
    Could you write me the script that would apply to my circumstance here? As I said in my post, this is my first week programming, I don't know how to do that, if it's not too much to ask for of course.
     
  6. dahiyabunty1

    dahiyabunty1

    Joined:
    Jan 22, 2020
    Posts:
    68
    addforce(maincam.transform.forward);
     
  7. LemonMontage420

    LemonMontage420

    Joined:
    May 28, 2020
    Posts:
    56
    This made the object fly out of my hands, I want it to rotate in the same orientation no matter which side im looking at.
     
  8. Cannist

    Cannist

    Joined:
    Mar 31, 2020
    Posts:
    64
    You probably want something along the lines of
    Code (CSharp):
    1. transform.RotateAround(transform.position, Camera.main.forward, 3f);
    Although there is a chance that
    Camera.main.forward
    is not what you actually want, but perhaps
    Camera.main.up
    or even just a world vector like
    Vector3.up
    .
     
  9. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    I guess I'm a bit confused becuase if you were rotating the camera to rotate the object, but the object wasnt moving, eventually the object would go off the screen (if you rotate the camera far enough). Is that what u want?

    Or were you trying to make the pickup stay infront of the players camera as they rotate the camera around? Because that's more common behaviour.

    Code (CSharp):
    1. Quaternion RotationDelta;
    2. Transform cameraTransform;
    3. Quaternion previousCameraRotation;
    4. Transform pickupTransform;
    5.  
    6. void Start()
    7. {
    8.     previousCameraRotation = cameraTransform.rotation;
    9. }
    10.  
    11. void Update()
    12. {
    13.      // Get the amount the camera rotated from the last frame
    14.      RotationDelta = cameraTransform.rotation - previousCameraRotation
    15.      
    16.      // Save the rotation this frame to compare with next frame
    17.      previousCameraRotation = cameraTransform.rotation
    18.  
    19.      // Apply the rotation to your pickup
    20.      pickupTransform.rotation *= RotationDelta;
    21.  
    22. }
    This should rotate the object the same amount that the camera rotates... though you probably only want it to rotate on the y axis..
     
    Last edited: Jun 10, 2020
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    A lot of confusion in this thread. Would you mind providing some pictures to illustrate the behavior you're actually looking for?
     
  11. LemonMontage420

    LemonMontage420

    Joined:
    May 28, 2020
    Posts:
    56
    Like This:
     

    Attached Files:

  12. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Seems like you want to rotate the object in the transforms directions. So instead of Vector3.up, .down. right, try the rotating on the up, down and right of the objects transform that you are rotating.
     
  13. LemonMontage420

    LemonMontage420

    Joined:
    May 28, 2020
    Posts:
    56
    And how would that look like in the context of my code? (Again I've been learning for 9 days now)
     
  14. dahiyabunty1

    dahiyabunty1

    Joined:
    Jan 22, 2020
    Posts:
    68
    pls tell us by making mockup what you want to do??