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 Olgo · Nov 15, 2012 at 10:53 PM · cameracharactermotion

Moving a character in relation to camera position?

Hi everybody,

I'm following some tutorials on creating a game from scratch. I'll paste the character movement script below. It takes input from a player input script and mob AI script to control your player and mobs, respectively.

I've been trying to modify the movement to move my character forward backward left right always in relation to the camera. Cant quite seem to get it to work. I'm using snippets from Unity's ThirdPersonController to help me but I'm stuck.

EDIT: I keep cutting this down so the script looks less daunting, I think thats why I'm getting no replies. I'm sure the answer is a quick edit of the script I have left below though.

Thanks!

Here's the script I'm attempting to modify:

     using UnityEngine;
     using System.Collections;
     
     [RequireComponent (typeof(CharacterController))]
     public class AdvancedMovement : MonoBehaviour {
         public enum State {
             Idle,
             Init,
             Setup,
             Run
         }
         
         public enum Turn {
             left = -1,
             none = 0,
             right = 1
         }
         public enum Forward {
             back = -1,
             none = 0,
             forward = 1
         }
         
             Init();
         }
         
         private void Init(){
             if(!GetComponent<CharacterController>())return;
             if(!GetComponent<Animation>())return;
             
             _state = AdvancedMovement.State.Setup;
         }
         
         private void ActionPicker(){
     
                 _myTransform.Rotate(0, (int)_turn * Time.deltaTime * rotateSpeed, 0);
     
                 if(_controller.isGrounded || _isSwimming) {
                     airTime = 0;
                     
                     _moveDirection = new Vector3(0, 0, (int)_forward);
     //                _moveDirection = Vector3.forward * Input.GetAxis("Move Forward");
                     _moveDirection = _myTransform.TransformDirection(_moveDirection).normalized;        //returns a vector of length 1 which we are trying to move
                     _moveDirection *= walkSpeed;
                 
                     if(_forward != Forward.none){
                         if(_isSwimming){
                         Swim ();
                         }
                         else{
                             if(_run){
                                 _moveDirection *= runMultiplier;
                                 Run();
                         }
                             else{
                                 Walk ();
                             }
                         }
     
                     }
                     else{
                         if(_isSwimming){
                             Swim();
                         }
                         else
                             Idle ();
                     }
                 
                     
         public void MoveMeForward(Forward z){
             _forward = z;
         }
         
         public void RotateMe(Turn y){
             _turn = y;
         }
     }
 
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 Olgo · Nov 15, 2012 at 10:54 PM 0
Share

also, I'm not sure how to give my script box a vertical slider.. sorry about the length

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Montraydavis · Nov 16, 2012 at 04:08 PM

If you mean having the camera follow the player, you can aimply attach the camera to the player, and position the angle to your degree of desire.

Or am I misunderstanding your question.?

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 Olgo · Nov 16, 2012 at 05:58 PM 0
Share

Sorry, I guess my question isn't as clear as I thought.

$$anonymous$$y camera orbits around the character and can be rotated using the mouse.

I'm looking to make my W,A,S,D movement use the camera's rotation. i.e., if the camera is facing east, and you press A, the character rotates left and moves north.

W should always be away from the camera, D would be always towards the camera, A and S would be left and right from the camera's view.

It would use this portion of the stock ThirdPersonController script:

var cameraTransform = Camera.main.transform; // Forward vector relative to the camera along the x-z plane var forward = cameraTransform.TransformDirection(Vector3.forward); forward.y = 0; forward = forward.normalized;a

// Right vector relative to the camera // Always orthogonal to the forward vector var right = Vector3(forward.z, 0, -forward.x);

var v = Input.GetAxisRaw("Vertical"); var h = Input.GetAxisRaw("Horizontal");

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

Vibrating/Blur Character 1 Answer

How to use Camera motion blur (oculus rift DK2) 0 Answers

A Whole Bunch of Third-Person Controller Issues 0 Answers

how to position the camera? pls help. 0 Answers

Camera animation into a scene –most efficient way? 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