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’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Bug Character dosen't move

Discussion in 'Input System' started by mdj12, May 21, 2023.

  1. mdj12

    mdj12

    Joined:
    Nov 10, 2021
    Posts:
    8
    Hello I make a game where player can upgrade their car by deattach car parts from car body. I writed a player controller and when I hit play button and press WASD for move nothing happens
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using NWH.VehiclePhysics2;
    5. using System;
    6.  
    7. public class PlayerController : MonoBehaviour
    8. {
    9.     public float moveSpeed = 5f;
    10.     public float rotationSpeed = 120f;
    11.     public float jumpForce = 5f;
    12.     public Transform cameraTransform;
    13.     public GameObject phoneModel;
    14.     public GameObject screwdriverBox;
    15.     public NWHVehicleController carController;
    16.     public GameObject steeringWheelIcon;
    17.     public GameObject carDrivingUI;
    18.  
    19.     private Rigidbody rb;
    20.     private bool isJumping = false;
    21.     private bool isDriving = false;
    22.     private bool isInCar = false;
    23.     private GameObject currentCar;
    24.     private bool isInToolMode = false;
    25.     private readonly GameObject carriedObject;
    26.  
    27.     private void Start()
    28.     {
    29.         rb = GetComponent<Rigidbody>();
    30.     }
    31.  
    32.     private void Update()
    33.     {
    34.         if (!isDriving)
    35.         {
    36.             // Movement
    37.             float horizontalInput = Input.GetAxis("Horizontal");
    38.             float verticalInput = Input.GetAxis("Vertical");
    39.  
    40.             Vector3 moveDirection = (horizontalInput * transform.right + verticalInput * transform.forward).normalized;
    41.             rb.velocity = new Vector3(moveDirection.x * moveSpeed, rb.velocity.y, moveDirection.z * moveSpeed);
    42.  
    43.             // Rotation
    44.             float mouseX = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
    45.             transform.Rotate(Vector3.up * mouseX);
    46.  
    47.             // Jump
    48.             if (Input.GetKeyDown(KeyCode.Space) && !isJumping)
    49.             {
    50.                 rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    51.                 isJumping = true;
    52.             }
    53.  
    54.             // Enter/exit car
    55.             if (isInCar)
    56.             {
    57.                 if (Input.GetKeyDown(KeyCode.E))
    58.                 {
    59.                     if (currentCar != null)
    60.                     {
    61.                         isDriving = true;
    62.                         carController.DrivenBy = gameObject;
    63.                         carController.ApplyControlInput(true);
    64.                         carDrivingUI.SetActive(true);
    65.                     }
    66.                 }
    67.             }
    68.  
    69.             // Interaction
    70.             if (Input.GetKeyDown(KeyCode.U))
    71.             {
    72.                 UseObject();
    73.             }
    74.  
    75.             if (Input.GetKeyDown(KeyCode.Mouse0))
    76.             {
    77.                 if (isInToolMode)
    78.                 {
    79.                     TakeScrewdriver();
    80.                 }
    81.                 else
    82.                 {
    83.                     TakeObject();
    84.                 }
    85.             }
    86.  
    87.             if (Input.GetKeyDown(KeyCode.Mouse1))
    88.             {
    89.                 ThrowObject();
    90.             }
    91.  
    92.             if (Input.GetKeyDown(KeyCode.Mouse2))
    93.             {
    94.                 RotateObject();
    95.             }
    96.  
    97.             // Tool mode
    98.             if (Input.GetKeyDown(KeyCode.Alpha1))
    99.             {
    100.                 if (!isInToolMode)
    101.                 {
    102.                     isInToolMode = true;
    103.                     screwdriverBox.SetActive(true);
    104.                 }
    105.                 else
    106.                 {
    107.                     isInToolMode = false;
    108.                     if (carriedObject != null)
    109.                     {
    110.                         DropObject();
    111.                     }
    112.                     screwdriverBox.SetActive(false);
    113.                 }
    114.             }
    115.         }
    116.         else
    117.         {
    118.             // Car driving mode
    119.             if (currentCar != null)
    120.             {
    121.                 if (Input.GetKeyDown(KeyCode.Return))
    122.                 {
    123.                     isDriving = false;
    124.                     carController.DrivenBy = null;
    125.                     carController.ApplyControlInput(false);
    126.                     carDrivingUI.SetActive(false);
    127.                 }
    128.             }
    129.         }
    130.     }
    131.  
    132.     private void ThrowObject()
    133.     {
    134.         throw new NotImplementedException();
    135.     }
    136.  
    137.     private void UseObject()
    138.     {
    139.         throw new NotImplementedException();
    140.     }
    141.  
    142.     private void TakeScrewdriver()
    143.     {
    144.         throw new NotImplementedException();
    145.     }
    146.  
    147.     private void RotateObject()
    148.     {
    149.         throw new NotImplementedException();
    150.     }
    151.  
    152.     private void TakeObject()
    153.     {
    154.         throw new NotImplementedException();
    155.     }
    156.  
    157.     private void DropObject()
    158.     {
    159.         throw new NotImplementedException();
    160.     }
    161.  
    162.     private void OnCollisionEnter(Collision collision)
    163.     {
    164.         if (collision.gameObject.CompareTag("Ground"))
    165.         {
    166.             isJumping = false;
    167.         }
    168.     }
    169.  
    170.     private void OnTriggerEnter(Collider other)
    171.     {
    172.         if (other.CompareTag("Car"))
    173.         {
    174.             isInCar = true;
    175.             currentCar = other.gameObject;
    176.             steeringWheelIcon.SetActive(true);
    177.         }
    178.     }
    179. }
    180.  
    Here is the source code (I use NWH vehicle Physics) Screenshot 2023-05-21 170254.png Screenshot 2023-05-21 170344.png
    And Input manager config