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

Applying force to a rigidbody relative to the direction of another GameObject and player input

Discussion in 'Physics' started by JamesGamesInc, Apr 12, 2020.

  1. JamesGamesInc

    JamesGamesInc

    Joined:
    Apr 12, 2020
    Posts:
    3
    Hi,

    So as the title suggests, I'm looking to find a way to add force to a rigid-body, based on the direction of another object, using player input. For example, if the player pressed w then it would move towards the forward direction the other game object is looking. Same for a, s and d respectively.

    This is what I have so far. Please note that is is designed to work in VR hence the first two lines. This shouldn't affect the input as I am using D-pad input.

    Code (CSharp):
    1. //Set size and position of the capsule collider so it maches our head.
    2.         Collider.height = Head.transform.localPosition.y;
    3.         Collider.center = new Vector3(Head.transform.localPosition.x, Head.transform.localPosition.y / 2, Head.transform.localPosition.z);
    4.  
    5.         moveDirection = new Vector3(0, 0, 0);//(Head.transform.forward*trackpad);
    6.         if (StandardisedControlScript.Dpad(Hand, "w"))
    7.         {
    8.             moveDirection = new Vector3(moveDirection.x+1, moveDirection.y, moveDirection.z);
    9.         }
    10.         if (StandardisedControlScript.Dpad(Hand, "a"))
    11.         {
    12.             moveDirection = new Vector3(moveDirection.x, moveDirection.y, moveDirection.z+1);
    13.         }
    14.         if (StandardisedControlScript.Dpad(Hand, "s"))
    15.         {
    16.             moveDirection = new Vector3(moveDirection.x-1, moveDirection.y, moveDirection.z);
    17.         }
    18.         if (StandardisedControlScript.Dpad(Hand, "d"))
    19.         {
    20.             moveDirection = new Vector3(moveDirection.x, moveDirection.y, moveDirection.z-1);
    21.         }
    22.         gameObject.transform.rotation = Quaternion.Euler(new Vector3(0, Head.transform.rotation.y, 0));
    23.         Debug.Log(moveDirection);
    24.         updateInput();
    25.         //GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(new Vector3(0, Head.transform.rotation.y, 0)));
    26.         GetComponent<Rigidbody>().velocity = new Vector3(GetComponent<Rigidbody>().velocity.x/2, GetComponent<Rigidbody>().velocity.y, GetComponent<Rigidbody>().velocity.z / 2);
    27.         GetComponent<Rigidbody>().AddRelativeForce(moveDirection * accellSpeed);
    Any help will be appreciated. I've tried using AddForce and AddRelative force but despite much googling, I'm not sure I have been implementing them correctly.

    If you need more info on anything, please ask and I'll get back to you ASAP
     
    AntonioModer likes this.
  2. RobeKey

    RobeKey

    Joined:
    Oct 3, 2015
    Posts:
    15
    Hi JamesGamesInc,

    I've made a small demo script for this. Tell me if you have difficulty using it, or it's not quite what you want.

    Cheers.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class GameManager : MonoBehaviour
    5. {
    6.     [SerializeField]
    7.     private KeyCode resetSceneKeyCode;
    8.  
    9.     [SerializeField]
    10.     private KeyCode verticalMovementKeyCode;
    11.  
    12.     [SerializeField]
    13.     private KeyCode directionInputKeyCode;
    14.  
    15.     [SerializeField]
    16.     private KeyCode rollKeyCode;
    17.  
    18.     [SerializeField]
    19.     private Transform directionTransform;
    20.  
    21.     [SerializeField]
    22.     private Rigidbody movingRigidbody;
    23.  
    24.     [SerializeField]
    25.     private float movementForceScale;
    26.  
    27.     [SerializeField]
    28.     private ForceMode movementForceMode;
    29.  
    30.     [SerializeField]
    31.     private float directionInputScale;
    32.  
    33.     private Vector3 movementVector;
    34.  
    35.     private Vector3 directionInput;
    36.  
    37.     private void Update()
    38.     {
    39.         UpdateInput();
    40.         UpdateRotation();
    41.     }
    42.  
    43.     private void FixedUpdate()
    44.     {
    45.         var direction = directionTransform.TransformDirection(movementVector);
    46.  
    47.         movingRigidbody.AddForce(
    48.             direction * movementForceScale,
    49.             movementForceMode);
    50.     }
    51.  
    52.     private void UpdateRotation()
    53.     {
    54.         directionTransform.Rotate(
    55.             directionInput * directionInputScale * Time.deltaTime,
    56.             Space.World);
    57.     }
    58.  
    59.     private void UpdateInput()
    60.     {
    61.         UpdateResetInput();
    62.  
    63.         if (Input.GetKey(directionInputKeyCode))
    64.         {
    65.             movementVector = Vector3.zero;
    66.             UpdateDirectionInput();
    67.         }
    68.         else
    69.         {
    70.             directionInput = Vector3.zero;
    71.             UpdateMovementInput();
    72.         }
    73.     }
    74.  
    75.     private void UpdateResetInput()
    76.     {
    77.         if (Input.GetKey(resetSceneKeyCode))
    78.         {
    79.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    80.         }
    81.     }
    82.  
    83.     private void UpdateMovementInput()
    84.     {
    85.         movementVector.x = Input.GetAxisRaw("Horizontal");
    86.  
    87.         var isVerticalMovementKeyDown = Input.GetKey(verticalMovementKeyCode);
    88.         var verticalInputAxis = Input.GetAxisRaw("Vertical");
    89.         movementVector.y = isVerticalMovementKeyDown ? verticalInputAxis : 0;
    90.         movementVector.z = isVerticalMovementKeyDown ? 0 : verticalInputAxis;
    91.     }
    92.  
    93.     private void UpdateDirectionInput()
    94.     {
    95.         directionInput.x = Input.GetAxisRaw("Vertical");
    96.  
    97.         var isRollKeyDown = Input.GetKey(rollKeyCode);
    98.         var rollInputAxis = Input.GetAxisRaw("Horizontal");
    99.         directionInput.z = isRollKeyDown ? -rollInputAxis : 0;
    100.         directionInput.y = isRollKeyDown ? 0 : rollInputAxis;
    101.     }
    102. }
    upload_2020-4-12_16-16-24.png
     
    Last edited: Apr 12, 2020
  3. JamesGamesInc

    JamesGamesInc

    Joined:
    Apr 12, 2020
    Posts:
    3
    Works a treat. I'll go about converting it to work with my current system etc and let you know if there are any issues. Thanks for putting in the time to write that up
     
    RobeKey likes this.
  4. RobeKey

    RobeKey

    Joined:
    Oct 3, 2015
    Posts:
    15
    Good to hear :) Fixed a small bug, one of the KeyCodes was still hard-coded. Maybe someone else will also find this of use.