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
1
Question by Chenab · Mar 23, 2013 at 07:04 PM · movementcharactercontrollercharacter.move

How can I get my character to move and strafe relative to the way its pointing?

I originally used transform.Translate to handle movement but since that didn't account for collision I instead switched over to controller.Move. That does account for collisions but does not account for the direction the character is facing. using UnityEngine; using System.Collections;

 public class PlayerClass : MonoBehaviour
 {
     private float hspeed = 10f;
     private float vspeed = 0F;
     private float gravity = 1F;
     private float test = 0;
     private Vector3 moveDirection = Vector3.zero;
     private float hdirection;
     public bool isaccending=true;
     private float movspeed=0;
 
     // Use this for initialization
     public float movementSpeed = 10;
     public float turningSpeed = 60;
     void Start(){
         
     
     }
     void Update() {
         CharacterController controller = GetComponent<CharacterController>();
         float horizontal = Input.GetAxis("Horizontal") * turningSpeed * Time.deltaTime;
         transform.Rotate(0, horizontal, 0);
         float vertical = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
         float strafe = Input.GetAxis("Strafe")* movementSpeed * Time.deltaTime;
         Vector3 movement= new Vector3(-vertical, 0, strafe);
         
         controller.Move(movement);
         isaccending=controller.isGrounded;
         if(controller.isGrounded){
             vspeed =0;
             
         }else{
             vspeed-=gravity* Time.deltaTime;
         }
         
         controller.Move(new Vector3(0,vspeed,0));
         
         
     }
     
 }
Comment
Add comment · Show 1
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 robertbu · Mar 24, 2013 at 04:01 AM 0
Share

I think if you add the following line below line 25, you will get what you want.

 movement = transform.TransformDirection(movement);

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Rocky46 · Apr 22, 2014 at 12:35 AM

hey bro when you use "Vector3()" its basically world space, to get directions you need to use stuff like "transform.forward/up/right" which are normalized directional vectors (normalized vectors are vectors that have a value between 0-1 precalculated) so you can then multiply them nice and easily and get the correct direction and length/speed/..etc, hope this helps :D.

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

Answer by Kajos · Mar 24, 2013 at 08:56 AM

You could use the FPSInputController script with the Character Motor. If not, you could at least learn from it.

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

12 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

Related Questions

Character Controller not moving model at all, but acts like it is? 1 Answer

How to avoid speed slowdown in case of controller.Move(); 1 Answer

The controller.move does not work correctly 0 Answers

Animation not working correctly, character is going up 0 Answers

Rotate character to the moving direction problems? 2 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