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.

Resolved This code doesn't work

Discussion in 'Scripting' started by seldron333, Apr 4, 2019.

  1. seldron333

    seldron333

    Joined:
    Mar 9, 2019
    Posts:
    2
    I have this code but when I play it inputs don't work. I tried to put a log in the start and it works.

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

    public class PlayerControl : MonoBehaviour
    {
    public float MoveVelocity = 10;
    Rigidbody PlayerRigidbody;
    GameObject PlayerCamera;
    void Start()
    {
    PlayerCamera = GameObject.Find("PlayerCamera");
    PlayerRigidbody = GetComponent<Rigidbody>();
    }
    void Udpate()
    {
    if (Input.GetKey(KeyCode.W))
    {
    MoveForward();
    }
    if (Input.GetKey(KeyCode.S))
    {
    MoveBackwards();
    }
    if (Input.GetKey(KeyCode.A))
    {
    MoveLeft();
    }
    if (Input.GetKey(KeyCode.D))
    {
    MoveRight();
    }
    }
    void MoveForward()
    {
    PlayerRigidbody.AddForce(PlayerCamera.transform.forward * MoveVelocity * Time.deltaTime);
    }
    void MoveBackwards()
    {
    PlayerRigidbody.AddForce(-PlayerCamera.transform.forward * MoveVelocity * Time.deltaTime);
    }
    void MoveLeft()
    {
    PlayerRigidbody.AddForce(-PlayerCamera.transform.right * MoveVelocity * Time.deltaTime);
    }
    void MoveRight()
    {
    PlayerRigidbody.AddForce(PlayerCamera.transform.right * MoveVelocity * Time.deltaTime);
    }
    }
     
    Last edited: Jun 17, 2020
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    3,911
    You spelled Update wrong (Udpate). And since you're adding force, that should go in FixedUpdate instead of Update.

    Also, please use Code tags when posting code.
     
    Vryken, seldron333 and Joe-Censored like this.
  3. seldron333

    seldron333

    Joined:
    Mar 9, 2019
    Posts:
    2
    Thanks, it works now
     
    Last edited: Jun 2, 2021
unityunity