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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Accessing players rigidbody from a script not attached to the player

Discussion in 'Scripting' started by jaekx, Sep 9, 2015.

  1. jaekx

    jaekx

    Joined:
    Dec 15, 2014
    Posts:
    27
    I have a 3rd person camera ive coded to orbit, zoom and target. While in the "target" camera state I want to lock the players rotation (much like links targeting in zelda), however, the script is attached to my camera not my player.. How do I access my players rigidbody in a script that's attached to my camera? and what bit of code would lock my players rotation while targeted? rotation x = 0 y = 0?? THANK YOU!

    -Jaek
     
    khaled24 likes this.
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    If you're using c# it'd be something like the following. To access your players rigidbody from a script on your camera you could do something like:

    Code (CSharp):
    1. public Rigidbody playerRigidbody;
    2.  
    3.  
    4. void Start() {
    5.      //Reference your player's rigidbody component at the "Start" of the game
    6.      playerRigidbody = GameObject.Find("MyPlayersName_InTheHierarchy").GetComponent<Rigidbody>();
    7. }
    That will reference your main character's rigidbody and hold it in a variable. So now, you have a quick way to access the rigidbody component on your main character from the script on your camera. And you could do something like

    Code (CSharp):
    1. void Update() {
    2.  
    3.      playerRigidbody.mass = 270;
    4.  
    5. }
    As for holding your characters rotation, look into "Rigidbody Constraints". I forget what the exact commands are right now, but there's probably plenty of examples on google. Although if it is what I think you're wanting, you might simply have to set your players rotation to a specific rotation every Update. In that case, you'd do something like "myPlayerGameObject.transform.rotation = someOtherRotation". Rotations can get a bit complicated, so maybe ask again if you get stuck on that too. :) Good luck.
     
    Yellag and jaekx like this.
  3. jaekx

    jaekx

    Joined:
    Dec 15, 2014
    Posts:
    27
    I'm giving it a shot right now, thank you very much for the insightful response! I appreciate it!
     
  4. colbtron

    colbtron

    Joined:
    Sep 29, 2018
    Posts:
    4
    Did it work? If I’m not mistaken, you could also make your player object’s Rigidbody a static variable that can be accessed from anywhere. Something like this on your player movement script:

    Code (CSharp):
    1. public static RigidBody playerRigidBody;
    2.  
    3. void Start()
    4. {
    5. playerRigidBody = GetComponent<RigidBody>();
    6. }
     
    Last edited: Apr 17, 2019