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 /
  • Help Room /
avatar image
1
Question by Droid_Void · Jul 06, 2017 at 10:24 PM · cameramovement3dunity5platformer

How to walk in direction of the main camera?

Hello, I am creating a 3D third person platformer and I have a third person camera that rotates around the player with the mouse.

I have been trying for days to try and get this to work properly but I have not came anywhere close.

What I am trying to achive is for the player to walk in the direction that the camera is looking at. I am out of options and I am resorting to asking here.

Thank you in advance for any help, it is much appreciated.

 public class MoveTest : MonoBehaviour {
 
     public float walkSpeed = 10.0f;
 
     private CharacterController controller;
 
     private float verticalVelcoity;
     private float gravity = 30.0f;
     private float jumpForce = 20.0f;
 
     // Use this for initialization
     void Start ()
     {
         controller = GetComponent<CharacterController>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         //Jumping
         if (controller.isGrounded)
         {
             verticalVelcoity = -gravity * Time.deltaTime;
             if (Input.GetButton("Jump"))
             {
                 verticalVelcoity = jumpForce;
             }
         }
         else
         {
             verticalVelcoity -= gravity * Time.deltaTime;
         }
 
         //Movement
 
         Vector3 moveVector = Vector3.zero;
         moveVector.x = Input.GetAxis("Horizontal") * walkSpeed;
         moveVector.y = verticalVelcoity;
         moveVector.z = Input.GetAxis("Vertical") * walkSpeed;
         controller.Move(moveVector * Time.deltaTime);
 
 
     }
 
 }
 
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 dval · Jul 07, 2017 at 02:47 AM 0
Share

have you looked at the 3rd person User controller in the Standard Assets Example Project? It moves the character relative to the camera. It would be a good place to start if you don't want to use it directly.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PixelSpice · Jul 09, 2017 at 12:50 AM

Our project is actually a third person project, and getting relative camera/player information can be a pain sometimes. In our case, we have a camera that can orbit freely around the player, and all the input is relative to that camera.

In other words, if you push forward you go forward relative to the camera. If you push left the character walks to the left (but is still walking straight ahead.)

I'm not sure exactly what you're looking for, but it sounds like you are doing more or less the same thing. this is how we are doing it:

The way we do it is convert our input to an angle (we use an input value of x and y from a gamepad):

 float angle = Mathf.Atan2(x, y) * Mathf.Rad2Deg;

We then calculate the difference between the angle of the player and the angle of the camera (CameraRigTarget is a game object and used as a desired location for the camera rig itself):

 angleBetween = this.transform.eulerAngles.y - CameraRigTarget.transform.eulerAngles.y;
 NewAngle = angle - angleBetween;

We then rotate the CameraRigTarget and the character based on this (AimX is the x value of the camera control input, as this is also where we evaluate the X input and move the camera around the player):

 CameraRigTarget_Move.transform.RotateAround(this.transform.position, Vector3.up, (360f - NewAngle) + (AimX * LookSensHoriz * Time.deltaTime));
 this.transform.Rotate(0, NewAngle, 0);

Alternatively, if you are looking to make the character look where the camera is looking right now, all you need is two of those lines:

 angleBetween = this.transform.eulerAngles.y - CameraRigTarget.transform.eulerAngles.y;
 this.transform.Rotate(0, angleBetween, 0); 
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

174 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

Related Questions

camera movement on a sphere 0 Answers

How do I make a camera controller where I can click and drag the world around, regardless of what angle the camera is at? 0 Answers

Help! Collision will not work, but all others will 0 Answers

Issue with getting the mouse position in 3D and moving a gameobject on the y & z axis 0 Answers

My camera is supposed to move on the X and Y axis, but it also moves on the Z axis 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