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

Trying to get my charcter to move but the script wont work. Heres my script.

Discussion in 'Scripting' started by Itz_Joey_FTW, Mar 20, 2019.

  1. Itz_Joey_FTW

    Itz_Joey_FTW

    Joined:
    Mar 19, 2019
    Posts:
    1
    using UnityEngine;
    using System.Collections;
    public class move : MonoBehaviour
    {
    public float movespeed;
    // Update is called once per frame
    void Update()
    {
    if (Input.GetKey(KeyCode.LeftArrow))
    {
    transform.Translate(new Vector3(-movespeed, 0, 0) * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.RightArrow))
    {
    transform.Translate(new Vector3(movespeed, 0, 0) * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.UpArrow))
    {
    transform.Translate(new Vector3(0, movespeed, 0) * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.DownArrow))
    {
    transform.Translate(new Vector3(0, -movespeed, 0) * Time.deltaTime);
    }
    }
    }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Can you use code tags please. Also, what is the exact problem- e.g. compiler error/ character not moving/ character moving too fast/ moving in wrong direction?
     
  3. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    77
    Code (CSharp):
    1. public float movespeed;
    you don't initialize this variable anywhere, so any try to move the object with this variable ends in no movement at all. Do you change this variable in Inspector in runtime?