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 /
  • Help Room /
avatar image
0
Question by erdmozn · Jun 09, 2020 at 08:52 PM · camera-movementplayer movementlag

My character moves at different speed in build and runs?

Hello my character moves at different speeds everytime i run in editor. Also its different at build too! I am using deltaTime too but i dont know what i'm missing. I searched all over the internet but it doesnt say anything else then deltaTime. Also i have issues with characterController movements too. It is very laggy in editor but fine in build.. here is my move script.

 `using System.Collections;
 using UnityEngine;
 
 public class Player : MonoBehaviour
 {
     [RequireComponent(typeof(CharacterController))]
     [RequireComponent(typeof(PlayerState))]
 
 
     [System.Serializable]
     public class MouseInput
     {
         public Vector2 Damping;
         public Vector2 Sensitivity;
         public bool LockMouse;
     }
 
     [SerializeField]
     AudioController footSteps;
 
     [SerializeField]
     float minimumMoveTreshold;
 
     [SerializeField]
     Character settings;
 
     [SerializeField]
     MouseInput MouseControl;
 
     [Header("MoveRamps")]
     [SerializeField]
     float rIdle;
 
     [SerializeField]
     float rUp;
 
     [SerializeField]
     float rDown;
 
     [SerializeField]
     float rSprint;
 
     public PlayerAim PlayerAim;
 
     InputController playerInput;
     Vector2 mouseInput;
 
     float speed = 0f;
     float targetSpeed;
     float targetRamp;
     Vector3 previousPosition;
 
 
 
 
     private PlayerShoot m_PlayerShoot;
     public PlayerShoot PlayerShoot
     {
         get
         {
             if (m_PlayerShoot == null)
                 m_PlayerShoot = GetComponent<PlayerShoot>();
             return m_PlayerShoot;
         }
     }
 
 
     private PlayerState m_PlayerState;
     public PlayerState PlayerState
     {
         get
         {
             if (m_PlayerState == null)
                 m_PlayerState = GetComponent<PlayerState>();
             return m_PlayerState;
         }
     }
 
     private CharacterController m_MoveController;
     public CharacterController MoveController
     {
         get
         {
             if (m_MoveController == null)
                 m_MoveController = GetComponent<CharacterController>();
             return m_MoveController;
         }
     }
 
 
     void Awake()
     {
         playerInput = GameManager.Instance.InputController;
         GameManager.Instance.LocalPlayer = this;
 
         if (MouseControl.LockMouse)
         {
             Cursor.visible = false;
             Cursor.lockState = CursorLockMode.Locked;
         }
     }
 
 
     void Update()
     {
         Move();
         
     }
 
     private void LateUpdate()
     {
         LookAround();
     }
 
     private void LookAround()
     {
         mouseInput.x = Mathf.Lerp(mouseInput.x, playerInput.MouseInput.x, 1f / MouseControl.Damping.x);
         mouseInput.y = Mathf.Lerp(mouseInput.y, playerInput.MouseInput.y, 1f / MouseControl.Damping.y);
         transform.Rotate(Vector3.up * mouseInput.x * MouseControl.Sensitivity.x );
         PlayerAim.SetRotation(mouseInput.y * MouseControl.Sensitivity.y);
     }
 
     int idleCounter = 0;
     int idleWait = 250;
 
     void Move()
     {
         if (Mathf.Abs(playerInput.Vertical) <= 0.1f && Mathf.Abs(playerInput.Horizontal) <= 0.1f)
         {
             playerInput.IsWalking = false;
             if (idleCounter <= idleWait)
             {
                 if (speed < 0.1f)
                 {
                     speed = 0f;
                     playerInput.IsIdle = true;
 
                 }
                 else
                 {
 
                     idleCounter += 1;
                     targetSpeed = 0f;
                     targetRamp = rIdle;
                 }
             }
         }
         else
         {
             idleCounter = 0;
             playerInput.IsIdle = false;
 
             if (!playerInput.IsCrouching && !playerInput.IsSprinting)
                 playerInput.IsWalking = true;
             else
                 playerInput.IsWalking = false;
 
             if (playerInput.IsIdle)
             {
                 targetSpeed = 0f;
                 targetRamp = rIdle;
             }
 
             if (playerInput.IsCrouching)
 
             {
                 targetSpeed = settings.CrouchSpeed;
                 if (speed > targetSpeed)
                     targetRamp = rDown;
                 else
                     targetRamp = rUp;
             }
 
             if (playerInput.IsWalking)
             {
                 targetSpeed = settings.WalkSpeed;
                 if (speed > targetSpeed)
                     targetRamp = rDown;
                 else
                     targetRamp = rUp;
             }
 
             if (playerInput.IsSprinting)
             {
                 targetSpeed = settings.SprintSpeed;
                 targetRamp = rSprint;
             }
         }
 
         speed = Mathf.Lerp(speed, targetSpeed * settings.MoveMultiply, targetRamp  * Time.deltaTime);
         print("speed   " + speed);
 
         Vector2 direction = new Vector2(playerInput.Vertical, playerInput.Horizontal);
         print(playerInput.Vertical + "  " + playerInput.Horizontal);
 
         direction = direction.normalized;
         print("normalized   "+direction);
 
         direction = direction * speed * Time.deltaTime;
         print("afterMultiply   " + direction);
 
         MoveController.SimpleMove(transform.forward * direction.x + transform.right * direction.y );
 
         if (Vector3.Distance(previousPosition, transform.position) > minimumMoveTreshold)
             footSteps.Play();
 
         previousPosition = transform.position;
 
     }
 
 
 }

And this is my camera move script.

 using System;
 using System.Collections;
 using UnityEngine;
 
 public class ThirdPersonCamera : MonoBehaviour
 {
     [System.Serializable]
     public class CameraRig
     {
         public Vector3 CameraOffset;
         public float CrouchHeight;
         public float Damping;
     }
 
 
     [SerializeField]
     CameraRig defaultCamera;
     [SerializeField]
     CameraRig aimCamera;
 
     [SerializeField]
     float yChangeSpeed;
     [SerializeField]
     float zChangeSpeed;
 
 
     Transform cameraLookTarget;
     Player localPlayer;
 
     void Awake()
     {
         GameManager.Instance.OnLocalPlayerJoined += HandleOnLocalPlayerJoined;
     }
 
     void HandleOnLocalPlayerJoined(Player player)
     {
         localPlayer = player;
         cameraLookTarget = localPlayer.transform.Find("cameraLookTarget");
 
         if (cameraLookTarget == null)
             cameraLookTarget = localPlayer.transform;
     }
     
 
     void LateUpdate()
     {
         if (localPlayer == null)
             return;
 
         CameraRig cameraRig = defaultCamera;
 
         if (localPlayer.PlayerState.WeaponState == PlayerState.EweaponState.AIMING || localPlayer.PlayerState.WeaponState == PlayerState.EweaponState.AIMEDFIRING)
             cameraRig = aimCamera;
 
 
         float targetHeight = cameraRig.CameraOffset.y + (localPlayer.PlayerState.MoveState == PlayerState.EmoveState.CROUCHING ? cameraRig.CrouchHeight: 1.2f);
 
         Vector3 targetPosition = cameraLookTarget.position +
                                  
                                  localPlayer.transform.forward * cameraRig.CameraOffset.z +
                                  localPlayer.transform.right * cameraRig.CameraOffset.x;
 
         targetPosition.y = Mathf.Lerp(transform.position.y , targetPosition.y * targetHeight, yChangeSpeed * Time.fixedDeltaTime);
         //targetPosition.z = Mathf.Lerp(targetPosition.z, localPlayer.transform.forward.z * cameraRig.CameraOffset.z, zChangeSpeed * Time.fixedDeltaTime);
 
 
         Quaternion targetRotation = Quaternion.LookRotation(cameraLookTarget.position - targetPosition, Vector3.up);
 
         //transform.position = Vector3.Lerp(transform.position, targetPosition, cameraRig.Damping * Time.fixedDeltaTime);
         transform.position = Vector3.Lerp(transform.position, targetPosition, cameraRig.Damping * Time.fixedDeltaTime);
         transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, cameraRig.Damping * Time.fixedDeltaTime);
     }
 }
 


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

212 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

Related Questions

Multiple camera and camera relative player movement 0 Answers

how to make camera follow player after a delay 1 Answer

If player hits certain X value in opposition to Camera, shift camera question. 0 Answers

How do I stop my players movement when I press a button and then enable it after I let the button go? 0 Answers

Mouvement issue with mouse input. 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