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 GC1983 · Dec 03, 2012 at 05:16 AM · movementgetaxis

Third Person Controller Design

Ive been working on setting up a third person controller for a few hours now and cant quite figure out how to do it the way Im looking for. If I use Vector3() to assign the GetAxis(), I set up the x and z axes precisely. No matter what, the character will move in strict x and z axes. I want the character to roam freely. So, if I set up the W key to move forward, I want that key to move forward no matter the direction of the character.

So, what I want is to have W and S to move the player forward and backward no matter which way they face. And I want the character to rotate left and right if I use the A and D keys. Does anyone have an algorithm or an example to help me get this moving? To be clearer, I want it to work like World of Warcraft player movements.

Thanks!

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
1

Answer by MarkFinn · Dec 03, 2012 at 05:21 AM

You can use

 playercharacterobject.transform.forward

to get a vector 3 which is essentially the direction the character is facing (assuming your models/prefabs are sensibly aligned) and then multiply that by the speed you want to move (per frame or per fixed update), before adding it to the character's position.

E.g. : Stick this on any object in a scene to get the idea.

 using UnityEngine;
 using System.Collections;
 
 public class DumbController : MonoBehaviour {
 
     public float spd=10;
     public float rotSpd=25;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey(KeyCode.A)){transform.Rotate(new Vector3(0,Time.deltaTime*rotSpd, 0));}
         if (Input.GetKey(KeyCode.D)){transform.Rotate(new Vector3(0,-Time.deltaTime*rotSpd, 0));}
         if (Input.GetKey(KeyCode.W)){transform.position+=(transform.forward*spd*Time.deltaTime);}
         if (Input.GetKey(KeyCode.S)){transform.position-=(transform.forward*spd*Time.deltaTime);}
     }
 }
Comment
Add comment · Show 8 · 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 GC1983 · Dec 03, 2012 at 08:22 AM 0
Share

Hmm..able to give me an example to work with?

avatar image MarkFinn · Dec 03, 2012 at 09:37 AM 0
Share

Example added.

avatar image GC1983 · Dec 03, 2012 at 05:10 PM 0
Share

Very nice! Now here's the issue with this set up...he walks through the terrain. And no gravity is applicable.

avatar image MarkFinn · Dec 04, 2012 at 04:55 AM 1
Share

This is kind of a seperate issue, but to start does the object have a rigidbody component attached?

Depending on the exact details of your game and approach you may have to replace the simple updating of the transform.position with

rigidbody.AddForce(transform.forward*spd);
(Best done in FixedUpdate() rather than Update().)

When I was working on City of S$$anonymous$$m we had similar issues and ended up doing everything by hand ins$$anonymous$$d of using the Unity Physics.

avatar image GC1983 · Dec 04, 2012 at 05:23 AM 0
Share

Again, very helpful. =) But, check this. Is it bad to use a rigidbody and take away the character controller? I did that and replaced the CC with a capsule collider and left the code the way it was with the transform.position...its works perfectly. Is there anything I could be concerned with in the long run or could this be an efficient way to solve my issue?

Show more comments

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

10 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

Related Questions

Maintaining Run on direction change (double tap using GetAxis) 0 Answers

How to increase a slowing down speed? 1 Answer

Unity 2d mobile movement 0 Answers

Input.GetAxisRaw("horizontal") not working 1 Answer

Animated Texture based on Movement 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