Search Unity

Rotating Ball Player... or not.

Discussion in 'Getting Started' started by jonj4040, Jul 17, 2020.

  1. jonj4040

    jonj4040

    Joined:
    Jul 16, 2020
    Posts:
    2
    Hi I was following some tutorials on basic game objects to get acquainted with Unity and c#. I am brand new to each. anyway I've been piecing together things beyond the original tutorial, but have run into some roadblocks I could not figure out simply by searching google.

    Firstly I put a Lerp on my players velocity and angular velocity to quickly but gently bring it back to 0 when input was not detected. I did this so that the ball did not continue endlessly rolling off the map. It seems however that my methods have also reduced gravity's affect of the ball. The ball does still fall when not in contact with the ground, but it is no accelerating at a simulated 9.8g*mass. Secondly, when the ball is in the air it seems I can still apply a forward force. I would like to restrict input to only times when the ball is in contact with the ground I was seeing and array of potential different methods, form rayTrace to Physics.colliderCapsule, the latter of the two was notated as being obsolete.

    Lastly I also notice that my player, which is a ball, does not roll when moving, It's more like it is just being pushed and sliding around instead of rolling.

    anyway any advice on my endeavors would be greatly appreciated. Thankyou!

    Code (CSharp):
    1. using System;
    2. using System.Diagnostics.Tracing;
    3. using System.Security.Cryptography;
    4. using UnityEngine;
    5. using UnityEngine.UIElements;
    6. using UnityEngine.XR;
    7.  
    8. public class PlayerInput : MonoBehaviour
    9. {
    10.     public float speed = 20;
    11.     public Rigidbody rb;
    12.     public float horizontal;
    13.     public float vertical;
    14.     public float rotationSensitivity = 80;
    15.      
    16.     float pRotate = 0.0f;
    17.     void Start()
    18.     {
    19.         rb = GetComponent<Rigidbody>();
    20.     }
    21.  
    22.        // Update is called once per frame
    23.     void Update()
    24.     {
    25.         pRotate += Input.GetAxis("Mouse X") * rotationSensitivity * Time.deltaTime;
    26.         transform.eulerAngles = new Vector3(0.0f, pRotate);
    27.  
    28.         horizontal = Input.GetAxis("Horizontal");
    29.         vertical = Input.GetAxis("Vertical");
    30.  
    31.         Vector3 direction = new Vector3(horizontal, 0.0f, vertical);
    32.         rb.AddRelativeForce(direction*speed);
    33.  
    34.                      
    35.         if (Input.GetKeyUp(KeyCode.W))
    36.         {
    37.             Freeze();
    38.         }
    39.  
    40.         if(Input.GetKeyUp(KeyCode.A))
    41.         {
    42.             Freeze();
    43.         }
    44.         if (Input.GetKeyUp(KeyCode.S))
    45.         {
    46.             Freeze();
    47.         }
    48.         if (Input.GetKeyUp(KeyCode.D))
    49.         {
    50.             Freeze();
    51.         }
    52.         else
    53.         {
    54.             Freeze();
    55.         }
    56.     }
    57.  
    58.     void Freeze()
    59.     {
    60.      
    61.         var val = rb.velocity;
    62.         var vAr = rb.angularVelocity;
    63.         Vector3 desiredVelocity = new Vector3(0f, 0f, 0f);
    64.         Vector3 desiredAngularVelocity = new Vector3(0f, 0f, 0f);
    65.         rb.velocity = Vector3.Lerp(val, desiredVelocity, 3f*Time.deltaTime);
    66.         rb.angularVelocity = Vector3.Lerp(vAr, desiredAngularVelocity, 3f*Time.deltaTime);
    67.  
    68.     }
    69. }
    70.  
     
    Last edited: Jul 17, 2020