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 have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

New Input System and RigidBody

Discussion in 'Scripting' started by drndfx, Feb 27, 2020.

Thread Status:
Not open for further replies.
  1. drndfx

    drndfx

    Joined:
    Jul 3, 2013
    Posts:
    89
    Hello,

    I'm trying to apply RigidBody controls to a cube. The cube supposed to collide with the plane. See attached.
    But it is going through the plane.

    Tried to use the new Input System but not sure what is the problem. I turned of "use Gravity" because the cube was flying all over the place.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerMovement2 : MonoBehaviour
    7. {
    8. InputMaster controls;
    9. Vector2 move;
    10. Vector3 movement;
    11. public Rigidbody rb;
    12.  
    13. void Start () {
    14.         rb = this.GetComponent<Rigidbody>();
    15.     }
    16.  
    17. void Awake(){
    18.     controls = new InputMaster();
    19.     controls.Player.Movement.performed += ctx => move = ctx.ReadValue<Vector2>();
    20.     controls.Player.Movement.canceled += ctx => move = Vector2.zero;
    21. }
    22. void Update(){
    23.     movement = new Vector2(move.x, move.y);
    24.     //movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    25.          
    26. }
    27. void FixedUpdate(){
    28.         moveCharacter(movement);
    29.     }
    30. void moveCharacter(Vector3 direction){
    31.         rb.MovePosition((Vector3)transform.position + (direction * 5f * Time.deltaTime));
    32.     }
    33.  
    34.  
    35. void OnEnable(){
    36.     controls.Player.Enable();
    37. }
    38. void OnDisable(){
    39.     controls.Player.Disable();
    40. }
    41.  
    42.  
    43. }
    44.  
     

    Attached Files:

  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
  3. drndfx

    drndfx

    Joined:
    Jul 3, 2013
    Posts:
    89
    Thank you.
     
    orionsyndrome likes this.
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    thank you for that screenshot, it would be much harder to understand what went wrong without it
     
  5. oshawottking

    oshawottking

    Joined:
    Jul 6, 2018
    Posts:
    2
    I tested out this script on a block but sometimes the block doesn't respond to controls for a few seconds or the block moves in the same direction even when I'm not pressing a key. Why is my version of this code unreliable.
     
  6. drndfx

    drndfx

    Joined:
    Jul 3, 2013
    Posts:
    89
    This is my updated Rigidbody settings. It seems to work fine now. And here is an updated code

    Code (CSharp):
    1. void FixedUpdate(){
    2.     Vector3 mymovement = new Vector3(move.x * 5f * Time.deltaTime,0,move.y * 5f * Time.deltaTime  );
    3.     rb.MovePosition(transform.position + mymovement);
    4. }
     

    Attached Files:

  7. wanchester

    wanchester

    Joined:
    Jun 15, 2021
    Posts:
    11
    hey dude can u send the script it would mean a lot thnx in advance
     
  8. TwinGhosts

    TwinGhosts

    Joined:
    Mar 4, 2015
    Posts:
    2
    Slight necro, but it seems you're using Time.delta time in a fixedUpdate loop. You might wanna change that to a Time.fixedDeltaTime for the physics time step :)
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    It's just a necro responding to a user who's not posted since early last year TBH.

    Also, the information you provided isn't correct; so you know, Time.deltaTime is identical to Time.fixedDeltaTime during the FixedUpdate. :)
     
    APSchmidtOfOld likes this.
Thread Status:
Not open for further replies.