Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 GhostSJ · Aug 04, 2021 at 01:16 PM · rotationscreentoworldpointmouse positionfacingtop down

Unity 3D - Make player rotate based on the mouse position in New input system/Character Controller

So I've been trying to make a top down game with the new input system. Been trying to make the character rotate and look at the mouse's position. The player moves on the x and z axis, with the camera looking down from the y axis. I have basic movement down, but I can't get the character rotation working.


Rotation is handled within this method in my code: where mousePosition can get he mouse position correctly, and then mouseViewportPosition converts it to a vector3 for the player to use.


Still can't figure out how to get the player to look at the mouse position as rotation. I assume it's because of something with the conversion of vector2 to vector3 that's causing, but I am relatively new to unity. I only know that I need to make the character rotate on the x and z axis only, and this rotation need to be based on mouse position.


My current result looks like this: https://gfycat.com/equaldefiantblackbear

With my player Input Actions here:


alt text


Here's the full method code, this method is called within Update():

 void handleRotation()
     {
         // We're getting a Vector2, whereas we will need a Vector3
         // Get a z value based on camera, and include it in a Vector3
         Vector2 mousePosition = playerInput.CharacterControls.MousePosition.ReadValue<Vector2>();
 
         var mousePositionZ = _camera.farClipPlane * .5f;
 
         Vector3 mouseViewportPosition = _camera.ViewportToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, _camera.transform.position.y));
 
 
         Debug.Log("MousePos: " + mouseViewportPosition);
 
         Vector3 positionToLookAt;
         positionToLookAt.x = mouseViewportPosition.x;
         positionToLookAt.y = 0.0f;
         //positionToLookAt.z = currentMovement.z;
         positionToLookAt.z = mouseViewportPosition.z;
 
         Quaternion currentRotation = transform.rotation;
         
         Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt - transform.position);
         transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
         
     }



And here's the full code if anyone wants to see on my Player Character:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class AnimationAndMovementController : MonoBehaviour
 {
 
     PlayerInput playerInput;
     CharacterController characterController;
 
 
     Animator animator;
 
     int isWalkingHash;
     int isRunningHash;
 
     Camera _camera;
 
     Vector2 currentMovementInput;
     Vector3 currentMovement;
     Vector3 currentRunMovement;
 
     bool isMovementPressed;
     bool isRunPressed;
 
     float rotationFactorPerFrame = 15.0f;
     float runMultiplier = 3.0f;
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         _camera = Camera.main;
     }
 
     void Awake()
     {
         playerInput = new PlayerInput();
         characterController = GetComponent<CharacterController>();
         animator = GetComponent<Animator>();
 
         isWalkingHash = Animator.StringToHash("isWalking");
         isRunningHash = Animator.StringToHash("isRunning");
 
 
         playerInput.CharacterControls.Move.started += onMovementInput;
         playerInput.CharacterControls.Move.canceled += onMovementInput;
         playerInput.CharacterControls.Move.performed += onMovementInput;
 
         playerInput.CharacterControls.Run.started += onRun;
         playerInput.CharacterControls.Run.canceled += onRun;
 
     }
 
 
     void onRun(InputAction.CallbackContext context)
     {
         isRunPressed = context.ReadValueAsButton();
     }
 
 
     void onMovementInput(InputAction.CallbackContext context)
     {
 
         currentMovementInput = context.ReadValue<Vector2>();
         currentMovement.x = currentMovementInput.x;
         currentMovement.z = currentMovementInput.y;
 
         currentRunMovement.x = currentMovementInput.x * runMultiplier;
         currentRunMovement.z = currentMovementInput.y * runMultiplier;
 
         isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
 
     }
 
 
 
     void handleAnimation()
     {
         bool isWalking = animator.GetBool(isWalkingHash);
         bool isRunning = animator.GetBool(isRunningHash);
 
         if (isMovementPressed && !isWalking)
         {
             animator.SetBool(isWalkingHash, true);
         }
         else if (!isMovementPressed && isWalking)
         {
             animator.SetBool(isWalkingHash, false);
         }
 
         if ((isMovementPressed && isRunPressed) && !isRunning)
         {
             animator.SetBool(isRunningHash, true);
         }
         else if ((!isMovementPressed && !isRunPressed) && isRunning)
         {
             animator.SetBool(isRunningHash, false);
         }
         else if ((isMovementPressed && !isRunPressed) && isRunning)
         {
             animator.SetBool(isRunningHash, false);
         }
 
     }
 
 
     void handleGravity()
     {
         if (characterController.isGrounded)
         {
             float groundedGravity = -0.05f;
             currentMovement.y = groundedGravity;
             currentRunMovement.y = groundedGravity;
         }
         else
         {
             float gravity = -9.8f;
             currentMovement.y = gravity;
             currentRunMovement.y = gravity;
         }
     }
 
     void handle_isRunPressed()
     {
         if (isRunPressed)
         {
             characterController.Move(currentRunMovement * Time.deltaTime);
         }
         else
         {
             characterController.Move(currentMovement * Time.deltaTime);
         }
     }
 
     void handleRotation()
     {
         // We're getting a Vector2, whereas we will need a Vector3
         // Get a z value based on camera, and include it in a Vector3
         Vector2 mousePosition = playerInput.CharacterControls.MousePosition.ReadValue<Vector2>();
 
         var mousePositionZ = _camera.farClipPlane * .5f;
 
         Vector3 mouseViewportPosition = _camera.ViewportToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, _camera.transform.position.y));
 
 
         Debug.Log("MousePos: " + mouseViewportPosition);
 
         Vector3 positionToLookAt;
         positionToLookAt.x = mouseViewportPosition.x;
         positionToLookAt.y = 0.0f;
         //positionToLookAt.z = currentMovement.z;
         positionToLookAt.z = mouseViewportPosition.z;
 
 
         
 
 
         Quaternion currentRotation = transform.rotation;
         
         Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt - transform.position);
         transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
         
     }
 
 
     float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
     {
         return Mathf.Atan2(b.y - a.y, b.x - a.x) * Mathf.Rad2Deg;
     }
 
 
 
     // Update is called once per frame
     void Update()
     {
         handleRotation();
         handleAnimation();
         handleGravity();
 
         handle_isRunPressed();
     }
 
 
     void OnEnable()
     {
         playerInput.CharacterControls.Enable();
     }
 
     void OnDisable()
     {
         playerInput.CharacterControls.Disable();
     }
 
 
 }


mouse-poisiton.png (39.6 kB)
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

207 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

Related Questions

How do I make my character face towards the mouse cursor? 0 Answers

Cant get Player to rotate towards mouse position on specific axis 0 Answers

Tank Turret facing away from me 1 Answer

How to Make NPC Rotate Towards the Player With the Bullets Being Rotated as Well. 0 Answers

Relative Rotation to an Object 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