Search Unity

Script working on object but not on camera

Discussion in 'Scripting' started by necrogaia, Feb 16, 2018.

  1. necrogaia

    necrogaia

    Joined:
    Jan 18, 2018
    Posts:
    5
    I wrote this script in hope to manipulate camera translate position as well as rotation through WASD keys, however, the camera does not respond as wished to be. I have also attached this same script on a 3D object which was behaving as planned.

    Problem when tied to a secondary camera, it works, but when tied to a primary camera, it jams and does not responds to it.
    Why does it not work on the camera? Thanks

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class CameraController : MonoBehaviour {
    6.     public float SPEED = 5.0f;
    7.     void Start () {
    8.    
    9.     }
    10.  
    11.     void Update () {
    12.          if (Input.GetKeyDown(KeyCode.W))
    13.        {
    14.             Vector3 position = this.transform.position;
    15.             position.y++;
    16.             this.transform.position = position;
    17.         }
    18.         if (Input.GetKeyDown(KeyCode.S))
    19.         {
    20.             Vector3 position = this.transform.position;
    21.             position.y--;
    22.             this.transform.position = position;
    23.         }    
    24.         if (Input.GetKeyDown(KeyCode.A))
    25.             transform.Rotate(0, SPEED, 0);
    26.         if (Input.GetKeyDown(KeyCode.D))
    27.             transform.Rotate(0, -SPEED, 0);
    28.     }
    29.  }
    30.  
     
    Last edited: Feb 16, 2018
  2. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
  3. necrogaia

    necrogaia

    Joined:
    Jan 18, 2018
    Posts:
    5
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I can't imagine why the code wouldn't work. Is it doing something, but just not what you want? If so, what's it doing?
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Did you change the SPEED in the editor? Does it still = 5 in the editor?
     
  6. necrogaia

    necrogaia

    Joined:
    Jan 18, 2018
    Posts:
    5
    when script is attached to the camera, the camera bounces back to the original position whenever whichever key is pressed. The code works fine on other objects, just the camera that decides to stick to its original position and rotation no matter what.
     
  7. necrogaia

    necrogaia

    Joined:
    Jan 18, 2018
    Posts:
    5
    when changed in the editor, it will take in whatever user input is
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is there another script also controlling the camera? If not that, I'm not sure.
     
  9. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Just tried the script on a new project on the Camera and it worked fine? You must have another script accessing the Camera.

    Also when you say "Manipulate the position" do you wish for the camera to move whilst holding the key down? because the the moment while using

    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.W))
    You will have to press W over and over again to move it. But if you use

    Code (CSharp):
    1. if (Input.GetKey(KeyCode.W))
    you will only have to press and hold W for it to move. However as you're increasing it by 1 unit it will move to fast so you might want to look into

    Code (CSharp):
    1.          transform.Translate(Vector3.up * SPEED * Time.deltaTime); // delta time allows movement based on the current frames and you can create a new variable to adjust the speed you want
     
    Last edited: Feb 16, 2018
  10. necrogaia

    necrogaia

    Joined:
    Jan 18, 2018
    Posts:
    5
    The camera works fine when it's not the main camera for me, when I tie it to be the main camera the game console view from, the key does not do anything to it.
     
  11. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Try the script on a completely new project on the main camera, then check youroriginal if anything attached to it
     
  12. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    My bet is that there is a second script controlling the camera position.
    Look for any scripts that reference the camera, "main camera"
    IE:
    gameObject.tag = "Main Camera";
    GetComponent<Camera>()