Search Unity

Showcase Unity3D Movement script that works with RigidBody.

Discussion in 'Scripting' started by SodiiumOnYoutube, Jan 17, 2023.

  1. SodiiumOnYoutube

    SodiiumOnYoutube

    Joined:
    Jul 30, 2020
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SubsribeToSodiium:) : MonoBehaviour
    7. {
    8. // Create a Tag on your in-game terrain called OnTerrain
    9.     public float MoveSpeed,  HorizontalSpeed, gravityForce, JumpForce;
    10.     public bool OnTerrain;
    11.     public Rigidbody RB;
    12.  
    13. void OnTriggerEnter (collider other)
    14. {
    15. if (other.tag == "OnTerrain")
    16. {
    17. OnTerrain = true;
    18. }
    19. void OnTriggerExit (collider other)
    20. {
    21. OnTerrain = false;
    22. }
    23. }
    24.     {
    25.         //Character Movement
    26.               RB.velocity = new Vector3(Input.GetAxis("Horizontal") * MoveSpeed, RB.velocity.y, -MoveSpeed);
    27.  
    28.         //Jumping & Physics
    29.         Physics.gravity = (new Vector3(0, gravityForce, 0));
    30.         if (Input.GetKey(KeyCode.Space) && OnTerrain == true)
    31.         {
    32.             RB.AddForce(new Vector3(0, JumpForce, 0), ForceMode.Impulse);
    33.         }
    34. }
    (I'm a beginner, so feel free to point out any flaws)
    Link to my Youtube channel: https://www.youtube.com/channel/UCHl1gjM-E-CDeZQq3QeptJw