• 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
0
Question by ERStudios · Mar 22, 2021 at 11:25 AM · 3rd person controller

Player don't stop moving Pls help, thanks in advance.

using System.Collections; using System.Collections.Generic; using UnityEngine;

namespace EB { public class playerlocomotion : MonoBehaviour { Transform cameraobject;

     inputmano inputHandeler;

     Vector3 movedirection;

     [HideInInspector]
     public Transform mytransform;

     public new Rigidbody rigidbody;
     public GameObject normalcamera;

    [Header("Stats")]
     [SerializeField]
     float movementSpeed = 5;
     [SerializeField]
     float rotationSpeed = 10;


     void Start()
     {
         rigidbody = GetComponent<Rigidbody>();
         inputHandeler = GetComponent<inputmano>();
         cameraobject = Camera.main.transform;
         mytransform = transform;
     }

     public void Update()
     {
         float delta = Time.deltaTime;

         inputHandeler.TickInput(delta);
         movedirection = cameraobject.forward * inputHandeler.vertical;
         movedirection += cameraobject.right * inputHandeler.horizontal;
         movedirection.Normalize();

         float speed = movementSpeed;
         movedirection *= speed;

         Vector3 projectedVelocity = Vector3.ProjectOnPlane(movedirection, normalVector);
         rigidbody.velocity = projectedVelocity;

     }

     #region Movement
     Vector3 normalVector;
     Vector3 targetposition;

     private void HandleRotation(float delta)
     {
         Vector3 targetDir = Vector3.zero;
         float moveOverride = inputHandeler.moveamout;

         targetDir = cameraobject.forward * inputHandeler.vertical;
         targetDir += cameraobject.right * inputHandeler.horizontal;

         targetDir.Normalize();
         targetDir.y = 0;

         if (targetDir == Vector3.zero)
             targetDir = mytransform.forward;

         float rs = rotationSpeed;

         Quaternion tr = Quaternion.LookRotation(targetDir);
         Quaternion targetRotation = Quaternion.Slerp(mytransform.rotation, tr, rs * delta);

         mytransform.rotation = targetRotation;
     }
     #endregion 

 }

}

Other script

using System.Collections; using System.Collections.Generic; using UnityEngine;

namespace EB { public class inputmano : MonoBehaviour { public float horizontal; public float vertical; public float moveamout; public float mousex; public float mousey;

     Playercontroller inputactions;

     Vector2 movementinput;
     Vector2 camerainput; 

         public void Update()
     {
         if (inputactions == null)
         {
           inputactions = new Playercontroller();
           inputactions.PlayerMovement.movement.performed += inputactions => movementinput = inputactions.ReadValue<Vector2>();
           inputactions.PlayerMovement.camera.performed += i => camerainput = i.ReadValue<Vector2>();
           
         }

         inputactions.Enable();
     }

     private void OnDisabled()
     {
         inputactions.Disable();
     }

     public void TickInput(float delta)
     {
         MoveInput(delta);
     }


     private void MoveInput(float delta)
     {
         horizontal = movementinput.x;
         vertical = movementinput.y;
         moveamout = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
         mousex = camerainput.x;
         mousey = camerainput.y;

     }
 }

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

156 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

Related Questions

Hi , thanks for the nice welcome. 0 Answers

Third person controller and multiple player agents 0 Answers

Unity input stutter with new Input System and 3rd Person Starter Asset 2 Answers

Unexpected symbol `)' 0 Answers

Network connectionclose / Ban / Chat mute 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