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

Turn Camera / Move Object with Arrow Keys

Discussion in 'Scripting' started by tburns517, Feb 2, 2016.

  1. tburns517

    tburns517

    Joined:
    Jan 12, 2016
    Posts:
    48
    I've searched on the Unity forums and other sites for an answer, but I haven't found anything to help me solve my problem. What I would like to do is simply turn the camera using the left and right arrow keys, while only allowing force to be added using the up and down arrow keys. The code I have now seems close to imitating what I want. The problem is that while using the left and right arrow keys, force is still applied horizontally while the camera turns. If I delete this portion of code, I am able to turn the camera, however, this also deletes the force to move horizontally, regardless if the camera is facing that way and I press the up key. Here are the functions making this possible in PlayerController and CameraController:

    PlayerController:

    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         float moveHorizontal = Input.GetAxis("Horizontal");
    4.         float moveVertical = Input.GetAxis("Vertical");
    5.  
    6.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    7.  
    8.         rb.AddForce (movement * speed);
    9.     }


    CameraController:


    Code (CSharp):
    1.     void LateUpdate () {
    2.         transform.position = player.transform.position + offset;
    3.  
    4.         if(Input.GetKey(KeyCode.LeftArrow))
    5.         {
    6.             transform.RotateAround(player.transform.position, Vector3.up, -50 * Time.deltaTime);
    7.             offset = transform.position - player.transform.position;
    8.         }
    9.  
    10.         if (Input.GetKey(KeyCode.RightArrow))
    11.         {
    12.             transform.RotateAround(player.transform.position, Vector3.up, 50 * Time.deltaTime);
    13.             offset = transform.position - player.transform.position;
    14.         }
    15.     }
    Any help would be greatly appreciated!
     
  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    remove the horizontal float from the input axis from your force vector(movement).

    the input axis accepts both horizontal arrows and a and d.
     
    tburns517 likes this.
  3. tburns517

    tburns517

    Joined:
    Jan 12, 2016
    Posts:
    48
    You mean like this?:

    Code (CSharp):
    1.         void FixedUpdate()
    2.         {
    3.          
    4.             float moveVertical = Input.GetAxis("Vertical");
    5.  
    6.             Vector3 movement = new Vector3(0.0f, 0.0f, moveVertical);
    7.  
    8.             rb.AddForce (movement * speed);
    9.         }
    10.  

    After doing so the camera moves correctly, however, I can only move the ball up and down on the Z axis. Turning the camera doesn't affect the direction of the ball.


    EDIT:

    I figured it out. Here is the working code:


    Code (CSharp):
    1. public Camera mainCamera
    2.  
    3. void FixedUpdate()
    4.     {
    5.         float moveVertical = Input.GetAxis("Vertical");
    6.  
    7.         Vector3 movement = mainCamera.transform.forward * moveVertical * speed;
    8.  
    9.         rb.AddForce (movement);
    10.     }
     
    Last edited: Feb 2, 2016
  4. enveryurek

    enveryurek

    Joined:
    Dec 19, 2019
    Posts:
    2
    EDIT:
    I figured it out. Here is the working code:


    Code (CSharp):
    1. public Camera mainCamera
    2.  
    3. void FixedUpdate()
    4.     {
    5.         float moveVertical = Input.GetAxis("Vertical");
    6.  
    7.         Vector3 movement = mainCamera.transform.forward * moveVertical * speed;
    8.  
    9.         rb.AddForce (movement);
    10.     }
    Thank you so much.

    Is there any update about these codes?