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

Moving a ball in different directions.

Discussion in 'Physics' started by Jordvh, Mar 15, 2020.

  1. Jordvh

    Jordvh

    Joined:
    Feb 29, 2020
    Posts:
    2
    Hello Devs!

    I'm kinda new to unity and im really enjoying learning new things.

    At the moment im building a platform game, where the player is a sphere shape.

    I'm at the point where I can move my player, forward, backwards and sideways.
    I did this by adding a force to my rigidbody.

    At this point im kinda struggling.
    What I want is by pressing a or d, I rotate my ball and then if I press W, I roll in the dirrection the ball is facing.

    The first part of the code is a comment, I tried it with my own code and got stuck, tried a new code but simply could not figure out how to get it to work.

    At this point the ball is rotating but i cant figure out how to move in the "new" forward direction.

    Code (CSharp):
    1. /*
    2. using UnityEngine;
    3.  
    4. public class Speler_Beweging : MonoBehaviour
    5. {
    6.     public Rigidbody rb;
    7.     public float Speler_voorwaards = 100f;
    8.     public float Speler_Achterwaards = -10f;
    9.     public float Speler_Jump = 10f;
    10.     public float Speler_NaarRechts = 2f;
    11.     public float Speler_NaarLinks = -2f;
    12.     public bool isGrounded;
    13.  
    14.  
    15.  
    16.     void Start()
    17.     {
    18.  
    19.     }
    20.  
    21.  
    22.     void Update()
    23.     {
    24.        
    25.                if (Input.GetKey("w"))
    26.                 {
    27.               Debug.Log("Forwards");
    28.               rb.AddForce(0, 0, Speler_voorwaards * Time.deltaTime, ForceMode.VelocityChange);
    29.           }
    30.  
    31.              if (Input.GetKey("s"))
    32.               {
    33.             rb.AddForce(0, 0, Speler_Achterwaards * Time.deltaTime, ForceMode.VelocityChange);
    34.              }
    35.            if (isGrounded)
    36.               {
    37.            if (Input.GetKey("space"))
    38.                {
    39.          Debug.Log("Jumping");
    40.           rb.AddForce(0, Speler_Jump * Time.deltaTime, 0, ForceMode.Impulse);
    41.         }
    42.          }
    43.  
    44.  
    45.  
    46.  
    47.       if (Input.GetKey("a"))
    48.         {
    49.             rb.AddForce(Speler_NaarLinks * Time.deltaTime, 0 , 0, ForceMode.VelocityChange);
    50.         }
    51.         if (Input.GetKey("d"))
    52.         {
    53.             rb.AddForce(Speler_NaarRechts * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    54.         }
    55.  
    56. }
    57.  
    58.      void OnCollisionEnter(Collision collisionInfo)
    59. {
    60.    if (collisionInfo.gameObject.tag == "Grond")
    61. {
    62. isGrounded = true;
    63.        }
    64. }
    65.  
    66.  
    67. void OnCollisionExit(Collision collisionInfo)
    68.    {
    69.      if (collisionInfo.gameObject.tag == "Grond")
    70.    {
    71. isGrounded = false;
    72.        }
    73. }
    74.  
    75.    
    76.  
    77.  
    78.  
    79.     }
    80.  
    81.  
    82. */
    83.  
    84. using UnityEngine;
    85.  
    86. public class Speler_Beweging : MonoBehaviour
    87. {
    88.     public Rigidbody rb;
    89.     public float moveSpeed = 10f;
    90.     public float turnSpeed = 10f;
    91.     private void Update()
    92.     {
    93.         if (Input.GetKey("w"))
    94.         {
    95.             Debug.Log("Forwards");
    96.             rb.AddForce(0, 0, moveSpeed * Time.deltaTime, ForceMode.VelocityChange);
    97.         }
    98.  
    99.         if (Input.GetKey("s"))
    100.         {
    101.             rb.AddForce(0, 0, -moveSpeed * Time.deltaTime, ForceMode.VelocityChange);
    102.         }
    103.  
    104.         if (Input.GetKey("d"))
    105.             transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
    106.  
    107.         if (Input.GetKey("a"))
    108.             transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
    109.     }
    110.  
    111.  
    112.  
    113.  
    114. }
     

    Attached Files:

  2. Jordvh

    Jordvh

    Joined:
    Feb 29, 2020
    Posts:
    2
    Oke I'm an idiot..

    After creating this topic I found out that there is a unity tut for this.

    Im sorry, i made it work with this code:

    Code (CSharp):
    1. public class Speler_Beweging : MonoBehaviour
    2. {
    3.     public Rigidbody rb;
    4.     public float moveSpeed = 10f;
    5.     public float turnSpeed = 10f;
    6.     void start()
    7.     {
    8.         rb = GetComponent<Rigidbody>();
    9.     }
    10.  
    11.     private void FixedUpdate()
    12.     {
    13.         float moveHorizontal = Input.GetAxis("Horizontal");
    14.         float moveVertical = Input.GetAxis("Vertical");
    15.  
    16.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    17.  
    18.         rb.AddForce(movement * moveSpeed);
    19.     }
    20. }