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. Dismiss Notice

How to Rotate a Camera? (C#)

Discussion in 'Editor & General Support' started by BlakeGillman, Jul 3, 2014.

  1. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    BTW Thanks daniel for your help on my last post (Lost internet)

    Thx for the help on my last post Daniel (Lost internet so couldn't reply)

    Anyways I am trying to give recoil to the main camera but it is not working. What am I doing wrong?

    Code (csharp):
    1.  
    2.     public GameObject playerCam;
    3.     public float camRotate;
    4.  
    5.         playerCam = gameObject.transform.FindChild("Main Camera").gameObject;
    6.         if(playerCam==null) {
    7.             Debug.LogError("Did not find any Camera in our children!");
    8.         }
    9.         camRotate = playerCam.transform.rotation.x;
    10.         camRotate = camRotate + weaponData.recoil;
    11.         playerCam.transform.rotation.x = camRotate;
    12.  
    This isn't all the code, just the area that I need help on.
    weaponData is another variable that access a seperate script

    ERROR:
    Assets/Networking/PlayerShooting.cs(56,37): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.rotation'. Consider storing the value in a temporary variable
     
  2. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    I have tried some more techniques, but I still can't figure it out D;

    My Objective:

    To make the camera rotate up a little to create a recoil effect.
     
  3. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    So, you are trying to access a quaternion directly. Since no-one alive in the world understands what a quaternion is, I'd encourage you to use the Rotate() API:

    http://docs.unity3d.com/ScriptReference/Transform.Rotate.html

    (The exact error message says that you are accessing the rotation member, but it's read-only, and you're not allowed to modify it.)
     
  4. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Thx, so, how would that work if I wanted to scale an object? (Like a bullet hole that i want to be a random size)

    Because i generate a random number and everything but I cant get it to actually scale to that number on the X and Y.
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    What does scaling bullet holes have to do with rotating a camera? Or is this a different question.
     
  6. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    It was a diff question

    But back to the point I tried
    Code (csharp):
    1.  
    2. gameObject.transform.Rotate(Vector3.up, 3);
    3.  
    But that didn't work..
    Idk, I looked at the reference you gave me, but I am having some trouble understanding it...
     
  7. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    I just want the camera to rotate up a certain amount (Which is determined by a variable)
    The X axis specifically if that helps
     
  8. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Rotating up would mean rotating along the x axis, which is Vector3.right.
    Code (csharp):
    1. gameObject.transform.Rotate(Vector3.right, -3);
    That would rotate it up 3 degrees.
     
    Last edited: Jul 3, 2014
  9. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    I am not sure why but that isn't working
     
  10. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Try making the number larger, you may not be noticing it.
     
  11. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    I figured out whats going on.

    Its somehow rotating the camera, but the view is not rotating.
    I put 20 there, and here are my results when shooting:



    You will notice how the shooting (mid of screen) is effected, but the camera / view itself isn't. So Ima little confused

    Like the Center of the Screen is being rotated, but the actual camera isn't. It's weird.
    (Center of Screen is where those 2 buttons are)
     
  12. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Also it doesn't rotate more, like when I shoot again its still that 20 degree. it doesnt rotate 20 more.

    Code (csharp):
    1.  
    2. playerCam.transform.Rotate(Vector3.right, weaponData.recoil);
    3.  
    weaponData is a reference to another script which recoil is set to 20.

    playerCam is set to the Main Camera of the player.
     
  13. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  14. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Sorry, I have limited internet and can't do videos.
     
  15. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Using the script on that reference
    That script actually makes the camera go up, it doesn't rotate but actually moves it upwards.

    I need to rotate?

    Did I use it wrong?

    Code (csharp):
    1.  
    2.         playerCam.transform.Translate(Vector3.up * Time.deltaTime);
    3.           // This may have been silly on my part but I also tried changing translate to rotate but that did nothing
    4.  
     
  16. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    That's Translate, not Rotate.

    That link discusses both movement and rotation.
     
  17. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    I found this:

    Code (csharp):
    1.  
    2.         playerCam.transform.Rotate(Vector3.up, -weaponData.recoil * Time.deltaTime);
    3.  
    but the camera isn't effected at all.
    *Recoil is even set to 20*
    Sorry if it seems like this should be obvious ;(

    Again the video may explain it,but I can't do videos because of my limited internet (I have to use a Mobile Hotspot)

    weaponData: A Variable which is linked to another script
    playerCam: The Players cam put as a GameObject in a variable
     
    Last edited: Jul 3, 2014
  18. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Unless that code is taking place every frame, it will only rotate it a tiny amount. At 60 fps Time.deltaTime would be 0.016, so you would be moving move the camera 0.32 degrees across the y axis (left/right)
     
  19. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    So your saying I just need to up the recoil? Like to 2000?
     
  20. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Code:

    Code (csharp):
    1.  
    2.         playerCam.transform.Rotate(Vector3.right, -weaponData.recoil * Time.deltaTime);
    3.  
    Result:


    So basically its doing that thing again where the center of the screen is rotating but oddly the camera is just not wanting to rotate up.
     
  21. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    And again shooting is not making it go up more, its stationary at that odd angle


    Would it be better if I just pasted my entire shoot script? (156 Lines)

    My Objective:
    Just to make so when I shoot the camera rotates up a little to represent recoil.
     
  22. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I'm not sure I understand the screen rotating vs the camera rotating.
     
  23. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Well take a look at that picture, oddly where i am shooting is rotating, but the actual screen isn't.
    And when I shoot more it doesn't rotate up more either.

    Here ill just give u my entire script and see if I f****d up somewhere:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerShooting : MonoBehaviour {
    6.  
    7.     float cooldown = 0;
    8.     FXManager fxManager;
    9.     WeaponData weaponData;
    10.     public GameObject playerCam;
    11.     public float camRotate;
    12.  
    13.     void Start() {
    14.         fxManager = GameObject.FindObjectOfType<FXManager>();
    15.  
    16.         if(fxManager == null) {
    17.             Debug.LogError("Couldn't find an FXManager.");
    18.         }
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update () {
    24.         cooldown -= Time.deltaTime;
    25.  
    26.         if(Input.GetButton("Fire1")) {
    27.             // Player wants to shoot...so. Shoot.
    28.             Fire ();
    29.         }
    30.  
    31.         if(Input.GetKeyDown ("r") && weaponData.ammo < weaponData.maxAmmo && weaponData.storedAmmo >= weaponData.remainAmmo) {
    32.             weaponData.storedAmmo = weaponData.storedAmmo - weaponData.remainAmmo;
    33.             weaponData.ammo = weaponData.maxAmmo;
    34.         }
    35.  
    36.     }
    37.  
    38.     void Fire() {
    39.         if(weaponData==null) {
    40.             weaponData = gameObject.GetComponentInChildren<WeaponData>();
    41.             if(weaponData==null) {
    42.                 Debug.LogError("Did not find any WeaponData in our children!");
    43.                 return;
    44.             }
    45.         }
    46.  
    47.         if(cooldown > 0) {
    48.             return;
    49.         }
    50.         if(weaponData.ammo <= 0) {
    51.             return;
    52.         }
    53.  
    54.         Debug.Log ("Firing our gun!");
    55.  
    56.         playerCam = gameObject.transform.FindChild("Main Camera").gameObject;
    57.         if(playerCam==null) {
    58.             Debug.LogError("Did not find any Camera in our children!");
    59.         }
    60.         camRotate = playerCam.transform.rotation.x;
    61.         camRotate = camRotate + weaponData.recoil;
    62.  
    63.         // HERE IS WHERE THE CODE
    64.         // IS BEING
    65.         // APPLIED
    66.         playerCam.transform.Rotate(Vector3.right, -weaponData.recoil * Time.deltaTime);
    67.         // ABOVE IS WHERE THE CODE
    68.         // IS BEING
    69.         // APPLIED
    70.  
    71.  
    72.         Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
    73.         Transform hitTransform;
    74.         Vector3   hitPoint;
    75.  
    76.         weaponData.ammo = weaponData.ammo - 1;
    77.         hitTransform = FindClosestHitObject(ray, out hitPoint);
    78.  
    79.         if(hitTransform != null) {
    80.             Debug.Log ("We hit: " + hitTransform.name);
    81.  
    82.             // We could do a special effect at the hit location
    83.             // DoRicochetEffectAt( hitPoint );
    84.  
    85.             Health h = hitTransform.GetComponent<Health>();
    86.  
    87.             while(h == null && hitTransform.parent) {
    88.                 hitTransform = hitTransform.parent;
    89.                 h = hitTransform.GetComponent<Health>();
    90.             }
    91.  
    92.             // Once we reach here, hitTransform may not be the hitTransform we started with!
    93.  
    94.             if(h != null) {
    95.                 // This next line is the equivalent of calling:
    96.                 //                   h.TakeDamage( damage );
    97.                 // Except more "networky"
    98.                 PhotonView pv = h.GetComponent<PhotonView>();
    99.                 if(pv==null) {
    100.                     Debug.LogError("Freak out!");
    101.                 }
    102.                 else {
    103.  
    104.                     TeamMember tm = hitTransform.GetComponent<TeamMember>();
    105.                     TeamMember myTm = this.GetComponent<TeamMember>();
    106.  
    107.                     if(tm==null || tm.teamID==0 || myTm==null || myTm.teamID==0 || tm.teamID != myTm.teamID ) {
    108.                         h.GetComponent<PhotonView>().RPC ("TakeDamage", PhotonTargets.AllBuffered, weaponData.damage);
    109.                     }
    110.                 }
    111.  
    112.             }
    113.  
    114.             if(fxManager != null) {
    115.  
    116.                 DoGunFX(hitPoint);
    117.             }
    118.         }
    119.         else {
    120.             // We didn't hit anything (except empty space), but let's do a visual FX anyway
    121.             if(fxManager != null) {
    122.                 hitPoint = Camera.main.transform.position + (Camera.main.transform.forward*100f);
    123.                 DoGunFX(hitPoint);
    124.             }
    125.  
    126.         }
    127.  
    128.         cooldown = weaponData.fireRate;
    129.     }
    130.  
    131.     void DoGunFX(Vector3 hitPoint) {
    132.         fxManager.GetComponent<PhotonView>().RPC ("SniperBulletFX", PhotonTargets.All, weaponData.transform.position, hitPoint);
    133.     }
    134.  
    135.  
    136.     Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint) {
    137.  
    138.         RaycastHit[] hits = Physics.RaycastAll(ray);
    139.  
    140.         Transform closestHit = null;
    141.         float distance = 0;
    142.         hitPoint = Vector3.zero;
    143.  
    144.         foreach(RaycastHit hit in hits) {
    145.             if(hit.transform != this.transform && ( closestHit==null || hit.distance < distance ) ) {
    146.                 // We have hit something that is:
    147.                 // a) not us
    148.                 // b) the first thing we hit (that is not us)
    149.                 // c) or, if not b, is at least closer than the previous closest thing
    150.  
    151.                 closestHit = hit.transform;
    152.                 distance = hit.distance;
    153.                 hitPoint = hit.point;
    154.             }
    155.         }
    156.  
    157.         // closestHit is now either still null (i.e. we hit nothing) OR it contains the closest thing that is a valid thing to hit
    158.  
    159.         return closestHit;
    160.  
    161.     }
    162. }
    163.  
    164.  
    165.  
     
    Last edited: Jul 3, 2014
  24. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Here is a screenshot of the whole Unity window if that helps:


    Notice how The shots are not at the center of the screen but are going up as if the camera was rotated, but as you can see the camera is not rotated.
     
  25. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Its kind of ridiculous how its this complicated just to rotate a camera through a script..
    Just make a camera rotate up some when I click.

    But Unity says no.
     
    Last edited: Jul 3, 2014
  26. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    By doing this though:

    Code (csharp):
    1.  
    2.         playerCam.transform.Rotate(Vector3.right, Random.Range (0, -weaponData.spread) * Time.deltaTime);
    3.         playerCam.transform.Rotate(Vector3.up, Random.Range (0, -weaponData.spread) * Time.deltaTime);
    4.  
    That makes a good spread system but no recoil
     
  27. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Are you sure you are rotating the correct object?
     
  28. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    It is set to the camera as you will see in that script it actually sets the variable inside the script to any child named "Main Camera"