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. Dismiss Notice

Move rigidbody forward that will be a player

Discussion in 'Scripting' started by Gradomir91, Jan 22, 2022.

  1. Gradomir91

    Gradomir91

    Joined:
    Dec 5, 2021
    Posts:
    6
    Hi Guys!
    I'm new to this stuff, checked lot's of topics, videos etc and probably there was an answer to my case but i did not understood (my english isn't perfect).

    I want to make my rigidbody player (ball with rotation) always move forward with same speed and only horizontal movement available. Tried lot's of different things and nothing helped.

    Can someone advise what should i do?
    For now i have something like that (removed not working autoforward code):


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SphereController : MonoBehaviour
    6. {  
    7.     [Header("Control Settings")]
    8.     [SerializeField]
    9.     public float speed = 1f;
    10.  
    11.  
    12.    
    13.     private Rigidbody rb;
    14.    
    15.  
    16.  
    17.  
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody>();
    23.        
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         float Hdirection;
    30.        
    31.  
    32.  
    33.         if ((Hdirection = Input.GetAxis("Horizontal")) != 0)
    34.         {
    35.          
    36.             rb.AddTorque(0, 0, -Hdirection * Time.deltaTime * speed);
    37.         }
    38.  
    39.      
    40.  
    41.      
    42.  
    43.  
    44.         }
    45.  
    46.  
    47. }
     
  2. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    was it stay in place while rotate?
     
  3. Gradomir91

    Gradomir91

    Joined:
    Dec 5, 2021
    Posts:
    6
    when i'm moving left/right it rotates.
    that "auto-forward" not set yet.
     
  4. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    the object need to be touch with surface(the ground). you can turn use gravity in rigidbody on to make sure the API you use work perfectly

    i test your code, not touch any surface is the problem
     
  5. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,114
    Something like this I think (I haven't used AddTorque before so I might be getting this wrong):

    Code (CSharp):
    1. public float rollForwardSpeed = 1f;
    2. public float turnSpeed = 0.2f;
    3.  
    4. private void FixedUpdate()
    5. {
    6.     HandleRollForward();
    7.     HandleRotate();
    8. }
    9.  
    10. private void HandleRollForward()
    11. {
    12.     rigidBody.AddTorque(transform.forward * rollForwardSpeed)
    13. }
    14.  
    15. private void HandleRotate()
    16. {
    17.     float turnInput = Input.GetAxis("Horizontal");
    18.     if(turnInput != 0f)
    19.     {
    20.         float torque = turnInput * turnSpeed;
    21.         rigidBody.AddTorque(transform.up * torque);
    22.     }
    23. }
     
    Gradomir91 likes this.
  6. Gradomir91

    Gradomir91

    Joined:
    Dec 5, 2021
    Posts:
    6
    Ball is moving on the ground. It moves left and right (rotates as well), but i have a problem with making a code that will make the ball move forward continuously with consant speed
     
  7. Gradomir91

    Gradomir91

    Joined:
    Dec 5, 2021
    Posts:
    6
    I will try that soon and let you know.
    Thanks!
     
  8. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    here a sample, you dont need if/else state
    Code (CSharp):
    1.  void Update()
    2.     {
    3.         float Hdirection = Input.GetAxis("Horizontal");
    4.         float forwardspeed = 5;
    5.  
    6.         rb.AddTorque(new Vector3(forwardspeed, 0, -Hdirection) * Time.deltaTime * speed) ;
    7.  
    8.  
    9.     }
     
    Gradomir91 likes this.
  9. Gradomir91

    Gradomir91

    Joined:
    Dec 5, 2021
    Posts:
    6
    Nice! It is moving forwad :)
    Thank you!

    Now the ball is moving really slow left/right but i will try to do something

    Ball was moving forward, but rotation was making it acting weird with direction (i think).
    But still it was better than many of mine attempts :)
     
  10. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    the speed you use, where you put it, it matter. make sure you done the right calculation for it.

    this should return your hDirection speed back to before
    Code (CSharp):
    1. rb.AddTorque(new Vector3(forwardspeed, 0, -Hdirection * speed) * Time.deltaTime) ;
     
    Gradomir91 likes this.
  11. Gradomir91

    Gradomir91

    Joined:
    Dec 5, 2021
    Posts:
    6
    Thanks. I will try to figure this out.
    At the moment i managed to move quite fast, but facing issues to move it even faster.
     
  12. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    it's all physics based, the faster the bus move the harder and longer to turn left or right.
    i dont working much with physics movement, sorry dont have the right formula for rb.AddTorque