Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I make an object move with my keyboard and how do I make the camera follow it?

Discussion in 'Getting Started' started by wall_gaudy676, Mar 11, 2023.

  1. wall_gaudy676

    wall_gaudy676

    Joined:
    Feb 4, 2022
    Posts:
    4
    tbh, I've never used unity. I am still 12, using scratch because I couldn't understand any other engine.

    And then I decided, "You know what? I think I should try unity! I can make better games in there and it would probably be as easy as scratch".


    The first 10 seconds I spent in the editor, I feel overwhelmed, confused.

    I tried to type:

    X = Cube
    Y = Cube


    But nothing happened

    I looked it up. And then I burnt my eyes as I forgot to turn off light mode.

    I checked the forums, but they had no answer.

    Please help me.
     
  2. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
  3. lorenzoFR

    lorenzoFR

    Joined:
    Mar 13, 2023
    Posts:
    2
    if you are uneasy with the scripting part, go take a look to Godot 3.5 ... you can do a lot with very little script.

    to answer your question, the code to move a GameObect look like this (the camera is a gameObject) :
    Code (CSharp):
    1. gameObjectToMove.transform.position = new Vector3(30, 50, 0)
    and you use the class Input to get the user input :
    https://docs.unity3d.com/ScriptReference/Input.html
     
    Last edited: Mar 14, 2023
  4. unity_B96C1AB55E636C25B71C

    unity_B96C1AB55E636C25B71C

    Joined:
    Mar 20, 2023
    Posts:
    1
    To make an object move with your keyboard and have the camera follow it in a game or interactive project, you will need to write code that responds to keyboard input and updates the position of the object and the camera. Here are the general steps to follow:

    1. Create an object in your game or project that you want to move with the keyboard. This could be a character, a vehicle, or any other interactive element.

    2. Add event listeners to your code that detect when certain keys are pressed on the keyboard. For example, you could use the keydown event to detect when the arrow keys or WASD keys are pressed.

    3. Write code that updates the position of your object based on the key inputs. For example, you could update the object's x and y coordinates by a certain amount when the arrow keys or WASD keys are pressed.

    4. Write code that updates the position of the camera based on the position of your object. For example, you could set the camera's x and y coordinates to be centered on the object's position.

    5. Add a loop to your code that updates the display at a regular interval, such as 60 times per second. This will ensure that the position of the object and camera are updated smoothly and continuously.
    Here's an example of code that moves an object with the arrow keys and updates the camera to follow it using JavaScript and the Canvas API:
    // Define the object to be moved
    let player = {
    x: 100,
    y: 100,
    speed: 5
    };

    // Define the canvas and camera dimensions
    let canvas = document.getElementById('myCanvas');
    let cameraWidth = canvas.width;
    let cameraHeight = canvas.height;

    // Add event listeners for arrow keys
    document.addEventListener('keydown', function(event) {
    if (event.code === 'ArrowUp') {
    player.y -= player.speed;
    }
    else if (event.code === 'ArrowDown') {
    player.y += player.speed;
    }
    else if (event.code === 'ArrowLeft') {
    player.x -= player.speed;
    }
    else if (event.code === 'ArrowRight') {
    player.x += player.speed;
    }
    });

    // Define the game loop
    function gameLoop() {
    // Update the camera position to follow the player
    let cameraX = player.x - (cameraWidth / 2);
    let cameraY = player.y - (cameraHeight / 2);
    // Draw the player object on the canvas
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.fillRect(player.x, player.y, 50, 50);
    // Schedule the next frame of the game loop
    requestAnimationFrame(gameLoop);
    }

    // Start the game loop
    requestAnimationFrame(gameLoop);


    This code sets up an object called player with x and y coordinates and a speed property. It adds event listeners for the arrow keys and updates the player's position accordingly. It then updates the camera position based on the player's position and draws the player object on the canvas. Finally, it sets up a game loop using requestAnimationFrame to continuously update the display. You can modify this code to suit your specific project and game mechanics.