I'm new to learning C#. How can I move an arrow one unit at a time?

Project screenshot

Here's the code I'm using now:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ArrowMovement : MonoBehaviour
    float directX;
    float directY;

    public float moveSpeed = 5f;

    private Rigidbody rb;

    private void Start()
        rb = GetComponent<Rigidbody>();


    private void FixedUpdate()
        float h = Input.GetAxis("Horizontal") * 100;
        float v = Input.GetAxis("Vertical") * 100;

        Vector3 vel = rb.velocity;
        vel.x = h;
        vel.z = v;
        rb.velocity = vel;

I just know how to do basic move


I am assuming your 3D models are in 1 unit scale as per unity.

Input.GetAxis will give you float values. Use Input.GetKeyDown("Key") instead.

Let's suppose you want to move up so you can use:

if (Input.GetKeyDown(KeyCode.UpArrow))
    transform.position += Vector3.Up;

Vector3.Up is Shorthand for writing Vector3(0, 1, 0) and 1 represent 1 unit. For more detail on representation of 3D vectors and points. click here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.