Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by SimonMK7 · Aug 15, 2020 at 03:42 AM · rigidbodycharactercontrollermovement script

Should i be using rigidbody or CharacterController for Controlling the player?

Currently making a top-down 3D shooter (with a bit of platforming mainly as an experiment) and have finally gotten around to getting it to work the way that I want it for my project. I ended up using a Rigidbody up till this point and I wanted to go ahead and implement slope movement. However, this tends to make the player slip down slopes fairly easily. I'll be adding my script bellow but in essence what would be a better way to implement slope movement for the player. should I use the Character Controller component instead or is there something that I can add to my current script to improve slope collision and movement?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SimonMK7 · Aug 15, 2020 at 03:47 AM

Here's the script for the player movement (hopefully the summary's don't make it unbearable to read).

 /// <summary>
 /// Select the camera to follow the player
 /// </summary>
 [Header("Player Camera:")]
 [Space,Tooltip("Select the camera to follow the player.")]
 public GameObject camera;


 /// <summary>
 /// The main player object
 /// </summary>
 [Header("Player Values:")]
 [Space,Tooltip("The main player object.")]
 public GameObject PlayerObj;


 /// <summary>
 /// the players jump speed
 /// </summary>
 [Space, Tooltip("the players jump speed")]
 public float jumpSpeed = 5f;


 /// <summary>
 /// determines whether the player is currently grounded or not.
 /// </summary>
 [Tooltip("determines whether the player is currently grounded or not.")]
 public bool OnGround = true;


 /// <summary>
 /// determines how many times the player has jumped.
 /// </summary>
 [Tooltip("determines how many times the player has jumped.")]
 public int currentJump = 0;
 public const int MAX_JUMP = 1;


 /// <summary>
 /// The given movement speed for the player object
 /// </summary>
 [Space, Tooltip("The given movement speed for the player object.")]
 [Range( 1f , 20f)]
 public float movementSpeed;


 /// <summary>
 /// The given rotation speed for the player object
 /// </summary>
 [Tooltip("the given rotation speed for the player object.")]
 [Range( 1f, 20f)]
 public float RotationSpeed;


 /// <summary>
 /// This determines wether the player is currently just moving or aiming while moving.
 /// Essentially changing movement styles on the fly.
 /// </summary>
 [Tooltip("This determines whether the player is currently just moving or aiming while moving. \nEssentially changing movement styles on the fly.")]
 public bool SwitchMovement;


 /// <summary>
 /// This is the ridged body component for the player
 /// </summary>
 [Tooltip("This is the ridged body component for the player.")] //use this unless proved problematic for slopes.
 private Rigidbody rb;


 /// <summary>
 /// For debugging purposes, _Movement & _Rotation will be temporary shown on the editor for testing (Not counting the ones for 3DS.
 /// </summary>
 [SerializeField] Vector3 _Movement;
 [SerializeField] Vector3 _Rotation;


 /// <summary>
 /// The GameObject used to determine where to spawn the players bullets.
 /// </summary>
 [Header("Player Bullet Component:")]
 [Space,Tooltip("The GameObject used to determine where to spawn the players bullets.")]
 public GameObject bulletSpawnPoint;


 /// <summary>
 /// The main bullet object
 /// </summary>
 [Tooltip("The main bullet object")]
 public GameObject Bullet;


 /// <summary>
 /// Instantiated the Bullet Object.
 /// </summary>
 private Transform BulletSpawned;


 /// <summary>
 /// Determines if the player is currently firing
 /// </summary>
 [Tooltip("Determines if the player is currently firing")]
 public bool isFiring;


 /// <summary>
 /// Determines the time in between shots
 /// </summary>
 [Tooltip("Determines the time in between shots")]
 [Range( 0f, 0.2f)]
 public float timeBetweenshots; //determines time in between shots
 [HideInInspector] float shotCounter; // timer of shots


 /// <summary>
 /// is the game currently paused?
 /// </summary>
 [Tooltip("is the game currently paused?")]
 public bool isPaused;


 /// <summary>
 /// is the game currently running?
 /// </summary>
 [Tooltip("is the game currently running?")]
 public bool isGamePlay; //Remove once object pooling is possible


 void Start()
 {
     rb = GetComponent<Rigidbody> ();
     //_controller = GetComponent<CharacterController>();
     //_PausedScript = GameObject.FindObjectOfType<Paused> ();
 }


 // ------- General Movement ------- //


 void DirectionMovement()
 {
     
     float horizontalMove = Input.GetAxisRaw ("Horizontal");
     float verticalMove = Input.GetAxisRaw ("Vertical");
 
     _Movement = new Vector3 (horizontalMove,0.0f,verticalMove);
 
     if (!SwitchMovement) 
     {
         if (_Movement != Vector3.zero) 
         {
             transform.rotation = Quaternion.Slerp (a: transform.rotation, b: Quaternion.LookRotation (_Movement), t: RotationSpeed * Time.deltaTime);
         }
         rb.MovePosition (transform.position + movementSpeed * Time.deltaTime * _Movement);
     } 
     else 
     {
         rb.MovePosition (transform.position + movementSpeed * Time.deltaTime * _Movement);
     }
 }


 // ------- Aiming Control type to switch on the fly ------- //


 void AimMovement()
 {
     _Rotation = new Vector3 (x: AimHori, y: 0.0f, z: AimVert);
 
     if (_Rotation != Vector3.zero) 
     {
         SwitchMovement = true;
         transform.rotation = Quaternion.Slerp (a: transform.rotation, b: Quaternion.LookRotation (_Rotation), t: RotationSpeed * Time.deltaTime);
     } 
     else 
     {
         SwitchMovement = false;
     }
 }


 // ------- new shooting type ------- //


 void Shoot()
 {
     if (isFiring && !isPaused) 
     {
         shotCounter -= Time.deltaTime;
         if (shotCounter <= 0) 
         {
             shotCounter = timeBetweenshots;
             BulletSpawned = Instantiate (Bullet.transform, bulletSpawnPoint.transform.position, Quaternion.identity);
             BulletSpawned.rotation = bulletSpawnPoint.transform.rotation;
         }
     } 
     else if (!isFiring && !isPaused)
     {
         shotCounter = 0;
     }

     if (Input.GetButton ("Fire"))
         isFiring = true;
     else 
         isFiring = false;
 }


 void Jump()
 {
     if (Input.GetButtonDown ("Jump") &&  (OnGround || MAX_JUMP > currentJump))
     {
         rb.AddForce (Vector3.up * jumpSpeed, ForceMode.Impulse);

         OnGround = false;
         currentJump++;
     }
 }


 void OnCollisionEnter (Collision _collision)
 {
     OnGround = true;
     currentJump = 0;
 }


 // Update is called once per frame
 void FixedUpdate () 
 {
     Shoot ();
     DirectionMovement ();
     AimMovement ();
     Jump ();
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

274 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player stuttering walking down ramps [3D] 0 Answers

Player still move left&right in MenuUI 0 Answers

Help converting a character controller script to rigidbody 1 Answer

About Character moviment methods 0 Answers

Spherical movement in relation to oriantation 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges