Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by ToeBeanGames · Apr 08, 2017 at 01:33 AM · c#rigidbodycharacter movement

Help with rigidbody character controller

So I'm working on a rigidbody character controller for a stealth platformer, similar to Sly Cooper. I've got some basic movement going, but the major two problems with it are 1: Whenever I stop moving, my character's rotation is reset to 0 2: The character moves in the direction the camera is facing no matter what, meaning that when the camera is pointed upwards my character will run into the sky.

Here's my code so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [AddComponentMenu("Controllers/Player Controller")]
 public class PlayerController : MonoBehaviour {
 
     public Animator playerAnimator;
     Transform cameraPos;
     CapsuleCollider col; //Collider
     Rigidbody phys; //Rigidbody
     float BaseHeight;//The default height of the character controller
     public float CrouchingHeight; //The height of the character controller while crouching
 
     public float RunSpeed = 5.0f;
     public float SprintSpeed = 10.0f;
     float CurrentSpeed = 0.0f;
 
     bool Grounded = true;
 
     // Use this for initialization
     void Start () {
 
         cameraPos = Camera.main.transform;
         col = GetComponent<CapsuleCollider> ();
         phys = GetComponent<Rigidbody> ();
         BaseHeight = col.height;
         
     }
     
     // Update is called once per frame
     void Update () {
         playerAnimator.SetFloat ("GroundSpeed", CurrentSpeed);
         playerAnimator.SetBool ("Grounded", Grounded);
 
         //Movement
         float h = Input.GetAxis ("Horizontal");
         float v = Input.GetAxis ("Vertical");
         Vector3 movementDir = new Vector3 (h, 0f, v);
 
         movementDir = cameraPos.TransformDirection (movementDir);
 
         transform.forward = movementDir;
 
         if (Grounded) {
             CurrentSpeed = RunSpeed * movementDir.magnitude;
             phys.velocity = movementDir * CurrentSpeed;
             
         }
     }
         
 }
 

Any help with this is appreciated. Thanks in advance.

Comment
Add comment · Show 3
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
avatar image ToeBeanGames · Apr 08, 2017 at 02:14 AM 0
Share

On the rotation issue, I noticed this pops up in the console when I stop moving

 Look rotation viewing vector is zero
 UnityEngine.Transform:set_forward(Vector3)
 PlayerController:Update() (at Assets/Scripts/Controllers/PlayerController.cs:45)

The camera I'm using for this is just the standard free look camera.

avatar image ToeBeanGames ToeBeanGames · Apr 08, 2017 at 02:21 AM 0
Share

I fixed the rotation issue, all i had to do was add if (movementDir != Vector3.zero) before setting transform.forward

EDIT: Now I am having an issue where my character starts spinning when they stop moving.

avatar image ToeBeanGames ToeBeanGames · Apr 08, 2017 at 05:37 AM 0
Share

As far as the movement issue goes, it only happens when moving forward and backward; my character stays aligned to the ground while moving left or right.

Here is the updated script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [AddComponent$$anonymous$$enu("Controllers/Player Controller")]
 public class PlayerController : $$anonymous$$onoBehaviour {
 
     public Animator playerAnimator;
     Transform cameraPos;
     CapsuleCollider col; //Collider
     Rigidbody phys; //Rigidbody
     float BaseHeight;//The default height of the character controller
     public float CrouchingHeight; //The height of the character controller while crouching
 
     public float RunSpeed = 5.0f;
     public float SprintSpeed = 10.0f;
     float CurrentSpeed = 0.0f;
 
     //Jump variables
     public float $$anonymous$$axJumpHeight = 1.5f;
 
     //State variables
 
     public float GroundCheckDistance = 0.1f; //How close to the ground is the player to be considered grounded?
     public float WallCheckDistance = 0.3f; //How close to a wall can the player be to start climbing?
     bool Grounded ()
     {
         return Physics.Raycast (transform.position, -Vector3.up, GroundCheckDistance);
     }
 
 
     // Use this for initialization
     void Start () {
 
         cameraPos = Camera.main.transform;
         col = GetComponent<CapsuleCollider> ();
         phys = GetComponent<Rigidbody> ();
         BaseHeight = col.height;
         
     }
     
     // Update is called once per frame
     void Update () {
         playerAnimator.SetFloat ("GroundSpeed", CurrentSpeed);
         playerAnimator.SetBool ("Grounded", Grounded());
         playerAnimator.SetFloat ("AirSpeed", phys.velocity.y);
 
 
         //$$anonymous$$ovement
         float h = Input.GetAxis ("Horizontal");
         float v = Input.GetAxis ("Vertical");
         Vector3 movementDir = new Vector3 (h, 0f, v);
 
         movementDir = cameraPos.TransformDirection (movementDir);
 
         if (movementDir != Vector3.zero)
             transform.forward = movementDir;
 
         if (Grounded()) {
             //$$anonymous$$ovement
             CurrentSpeed = RunSpeed * movementDir.magnitude;
             phys.velocity = movementDir * CurrentSpeed;
 
             //Jumping
             if (Input.GetButtonDown ("Jump")) {
                 phys.AddForce (Vector3.up * $$anonymous$$axJumpHeight, Force$$anonymous$$ode.Impulse);
             }
         }
             
     }
         
 }
 

0 Replies

· Add your reply
  • Sort: 

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

302 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to limit an object's position to a spherical radius? 0 Answers

[Solved] Is it okay if we change transform.position on a kinematic rigid body which also does not use Gravity? 2 Answers

Collider/Rigidbody issue.Camera shaking! 3 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