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 Ozmotix · Aug 11, 2021 at 12:06 AM · camerarotationmovementmouse

How do I move the player towards the direction of the camera using cinemachine?

Hi, Im new to coding and trying to get the CM vcam to work with my script so the player moves in the direction of the camera (in first person). How might I go about making it work with what I have going now? (Rotation and Look) section near the bottem.

 public class PlayerController : MonoBehaviour
     {
     
         //Refernce to Inputs / Charactercontroller Component on body
         private PlayerInputs playerInput;
         private CharacterController characterController;
         
     
     
         //Player Camera
         private Transform camTransform;
         private Vector2 lookDirection;
         private float rotationFactorPerFrame = 1.0f;
         //test
         private Vector3 startingRotation;
     
         private float clampAngle = 80f;
         private float horizontalSpeed = 10f;
         private float verticalSpeed = 10f;
     
         //Movement
         private Vector2 currentMovementInput;
         private Vector3 currentMovement;
         [Header("Movement")]
         public float movementSpeed;
         private bool isMovementPressed;    
     
         //Gravity is a harness...
         private float gravity = -3f;
         private float groundedGravity;
     
         //Jumping
         private bool isJumpPressed;
         private float initialJumpVelocity = 1.5f;
         private float maxJumpHeight = 4.0f;
         private bool isJumping = false;
         
     
     
         //Activating the Input system
         private void Awake()
         {       
     
             playerInput = new PlayerInputs();
             characterController = GetComponent<CharacterController>();
     
             playerInput.CharacterControls.Move.started += OnMovementInput;
             playerInput.CharacterControls.Move.performed += OnMovementInput;
             playerInput.CharacterControls.Move.canceled += OnMovementInput;
     
             playerInput.CharacterControls.Jump.started += OnJump;
             playerInput.CharacterControls.Jump.canceled += OnJump;
     
             playerInput.CharacterControls.Look.started += OnLook;
             playerInput.CharacterControls.Look.performed += OnLook;
             playerInput.CharacterControls.Look.canceled += OnLook;
         }
     
         private void Start()
         {
                    
             Cursor.lockState = CursorLockMode.Locked;
     
         }
     
         //For constant Moveing Attributes
         private void Update()
         {
             
             characterController.Move(currentMovement * Time.deltaTime * movementSpeed);        
             GravityHandler();
             JumpHandler();
     
         }
     
         //Gravity Listener
         private void GravityHandler()
         {
             bool isFalling = currentMovement.y <= 0.0f || !isJumpPressed;
             float fallMultiplier = 2.0f;
             if (characterController.isGrounded)
             {
                 float groundedGravity = -0.05f;
                 currentMovement.y = groundedGravity;
     
             }
             else if (isFalling)
             {
     
                 float previousYVelocity = currentMovement.y;
                 float newYVelocity = currentMovement.y + (gravity * fallMultiplier * Time.deltaTime);
                 float nextYVelocity = Mathf.Max((previousYVelocity + newYVelocity) * 0.5f, -20.0f);
                 currentMovement.y = nextYVelocity;
     
             }
             else
             {
                 float previousYVelocity = currentMovement.y;
                 float newYVelocity = currentMovement.y + (gravity * Time.deltaTime);
                 float nextYVelocity = (previousYVelocity + newYVelocity) * 0.5f;
                 currentMovement.y = nextYVelocity;           
     
             }
     
         }
     
         //Rotation & Look
         private void OnLook(InputAction.CallbackContext context)
         {
             
                 if (startingRotation == null) startingRotation = transform.localRotation.eulerAngles;
                 Vector2 deltaInput = GetMouseDelta();
                 startingRotation.x += deltaInput.x * verticalSpeed * Time.deltaTime;
                 startingRotation.y += deltaInput.y * horizontalSpeed * Time.deltaTime;
                 startingRotation.y = Mathf.Clamp(startingRotation.y, -clampAngle, clampAngle);
                 state.RawOrientation = Quaternion.Euler(-startingRotation.y, startingRotation.x, 0f);
     
         }
     
         private static Vector2 GetMouseDelta()
         {
            playerInput.CharacterControls.Look.ReadValue<Vector2>;
         }
 

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

0 Replies

· Add your reply
  • Sort: 

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

256 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 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

rotate camera on mouse reaching edges 1 Answer

Select/Drag/Drop Objects with a mouse 0 Answers

Move the camera when the mouse reaches the edges of the screen 1 Answer

Smooth Camera Movement script with problem. 0 Answers

Adjust camera rotation while looking at a target 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