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 /
avatar image
0
Question by Mightycam · Mar 18, 2015 at 12:55 PM · playervector3charactercontrollerplayer movementinput.getaxis

C# Player Movement help?

Hey guys I've done a very basic player movement script where a capsule can go back and forth and rotate to turn around and stuff. What I'm trying to find help in is how do I make it so that the player movement moves around like the 3rd person controller? So that it will always walk on the Z axis of the object and just do like static turns at 90° and 45° if 2 buttons are pressed So like if you press up - doesn't turn at all, just moves forward press right - will turn 90° and move forward press up + left - will turn 45° and move forward.

Any links that'll help me out or a tutorial or some kind of link will be greatly appreciated.

 public class CharacterControl : MonoBehaviour 
 {
     public float rightSpeed;
     public float forwardSpeed;
     public float turnSmoothing = 15f;
     private CharacterController playerController;
 
     void Start()
     {
         playerController = GetComponent<CharacterController>();
     }
 
     void Update()
     {
         Vector3 forward = transform.TransformDirection(Vector3.forward);
         float fspeed = forwardSpeed * Input.GetAxis("Vertical");
 
         Vector3 right = transform.TransformDirection(Vector3.right);
         float rspeed = forwardSpeed * Input.GetAxis("Horizontal");
 
         playerController.SimpleMove(fspeed * forward);
         playerController.SimpleMove(rspeed * right);
     }
 
     void Rotating (float horizontal, float vertical)
     {
         Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
 
         Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
 
 
         Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
 
 
         rigidbody.MoveRotation(newRotation);
     }
 }



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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by jesterswilde · Mar 19, 2015 at 07:37 AM

It sounds like you want the player movement to driven by the camera's facing. You also always want the player to face the direction they are going (no strafing)

I would use something like this to get the character to face their velocity

     public void LookTowardsVelocity(){ //Call this whenever you move
         Quaternion _oldRotation = _player.transform.rotation;  //save old rotation
         _player.transform.LookAt (_player.transform.position + _rigid.velocity); //Looks towards velocity
         _player.transform.rotation = Quaternion.Slerp (_oldRotation, _player.transform.rotation, Time.deltaTime * _lerpSpeed) //Lerps beteween old rot and new rot
     }

This will have your character rotating up if they jump or something like that so my implementation has a few more qualifiers. I also only use it when the character is jumping, which is what the velocity limits are about.

 protected virtual void LookTowardsVelocity(){ //called when the player jumps
         Vector3 _noYVel = new Vector3 (_player.Rigid.velocity.x, 0, _player.Rigid.velocity.z); 
         if(_noYVel.magnitude > 6){ //This prevents the player from spinning wildly in the air
             Vector3 _back = _player.transform.position + _noYVel; //It's called back because it's mostly for wall jumps
             _player.transform.LookAt (_back); //you'll note no lerping in this version. That's beacuse Snapping is important, I may put the lerping back in later
         }
     }


Then some simple movement relative to the camera

 void Movement(){ //gets called on fixed update
         Vector3 _forwardVector = _camera.transform.forward * Input.GetAxisRaw ("Vertical"); //get their forward direction
         Vector3 _rightVector = _camera.transform.right * Input.GetAxisRaw ("Horizontal"); //get theor left and right direction
         _player.Rigid.velocity = (_forwardVector + _rightVector).normalized * _player.speed; // Normalize the 2 vectors, and multiply them by the speed you want
         //If you don't always have flat ground, you will probably want to project the _fullVelocity vector over the ground at some point
         //You can use Add force, or set the velocity. The first gives you accelleration, the latter gives you tight controls. I use a hybrid, but this example just sets velocity
     }

https://www.youtube.com/watch?v=MPN5kYkdYUY&list=PL7AE076AFAFD3C305 This was one of the things I went through a bit of when I was first doing movement systems.

Comment
Add comment · Show 1 · 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
avatar image Mightycam · Mar 20, 2015 at 01:37 AM 0
Share

Sweet thanks for the tips guys, this is my first time ever trying to do a character control that isn't so basic so any tips would be great at the moment.

avatar image
0

Answer by siaran · Mar 19, 2015 at 12:00 AM

I don't have a link or tutorial, but let me see if I got what you want right:

You want your player to move in forward relative to the camera, when you press up, to move right relative to your camera is facing when you press right, with possibility to combine for a sort of right-forward sort of movement?

I'm not really in the mood to type up a script, but here is basically what you should do:

-Convert your input into a direction (i.e "up" = vector3.forward, "right" = vector3.right, "up"+"right" = vector3(0.5,0,0.5)) <- something like that, this is not a good way of doing that but you get the idea.

-that direction will be in world space, but you want it to be relative to your camera position, so, transform that direction to be relative to your camera. Look into Transform.TransformDirection and Transform.InverseTransformDirection, I'm too lazy to look up which one goes from world to local and which one is from local to world, I always forget.

-Now you have the direction you actually want to move your player in, so have your player, well... move in that direction.

-Of course, you'll also want to rotate your player object to actually face the direction it's moving in, but I'm not going into that here.

So yeah, basically you just need to make your movement direction relative to your camera rotation.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why my character moves left when i press right key ? Controls are upside down? 2 Answers

Rotate a vector3 to a surface normal? 1 Answer

3D Character Controller slowing down the higher the slope angle (both up and down) 1 Answer

Dashing stops when i let go of WASD keys 1 Answer

Audio for movement 1 Answer


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