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
0
Question by RADAR-SYSTEM · Jan 21, 2019 at 04:30 AM · animation script

Playing Animation while Moving

Hi I have a C# script that I dont know how to plug in a simple line of script that make's it so if player is moving then condition = 1 and if not moving the condition = 0. could some one make a line of script that plugs into this script with out screwing it up.

(btw condition 1 is the player animation run and condition 0 is player idle)

using System; using UnityEngine; using Random = UnityEngine.Random;

[RequireComponent(typeof(CharacterController))] [RequireComponent(typeof(AudioSource))] public class FirstPersonController_EXAMPLE : MonoBehaviour { [SerializeField] private bool m_IsWalking; [SerializeField] private float m_WalkSpeed; [SerializeField] private float m_RunSpeed; [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten; [SerializeField] private float m_JumpSpeed; [SerializeField] private float m_StickToGroundForce; [SerializeField] private float m_GravityMultiplier; [SerializeField] private MouseLook_EXAMPLE m_MouseLook; [SerializeField] private bool m_UseFovKick; [SerializeField] private bool m_UseHeadBob; [SerializeField] private float m_StepInterval; [SerializeField] private AudioClip[] m_FootstepSounds; // an array of footstep sounds that will be randomly selected from. [SerializeField] private AudioClip m_JumpSound; // the sound played when character leaves the ground. [SerializeField] private AudioClip m_LandSound; // the sound played when character touches back on ground.

 private Camera m_Camera;
 private bool m_Jump;

 private Vector2 m_Input;
 private Vector3 m_MoveDir = Vector3.zero;
 private CharacterController m_CharacterController;
 private CollisionFlags m_CollisionFlags;
 private bool m_PreviouslyGrounded;
 private Vector3 m_OriginalCameraPosition;
 private float m_StepCycle;
 private float m_NextStep;
 private bool m_Jumping;
 private AudioSource m_AudioSource;

 // Use this for initialization
 private void Start()
 {
     m_CharacterController = GetComponent<CharacterController>();
     m_Camera = Camera.main;
     m_OriginalCameraPosition = m_Camera.transform.localPosition;

     m_StepCycle = 0f;
     m_NextStep = m_StepCycle / 2f;
     m_Jumping = false;
     m_AudioSource = GetComponent<AudioSource>();
     m_MouseLook.Init(transform, m_Camera.transform);
 }


 // Update is called once per frame
 private void Update()
 {
     RotateView();
     // the jump state needs to read here to make sure it is not missed
     if (!m_Jump)
     {
         m_Jump = Input.GetButton("Jump");
     }



 }


 private void PlayLandingSound()
 {
     m_AudioSource.clip = m_LandSound;
     m_AudioSource.Play();
     m_NextStep = m_StepCycle + .5f;
 }


 private void FixedUpdate()
 {
     float speed;
     GetInput(out speed);
     // always move along the camera forward as it is the direction that it being aimed at
     Vector3 desiredMove = transform.forward * m_Input.y + transform.right * m_Input.x;

     // get a normal for the surface that is being touched to move along it
     RaycastHit hitInfo;
     Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
                        m_CharacterController.height / 2f);
     desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;

     m_MoveDir.x = desiredMove.x * speed;
     m_MoveDir.z = desiredMove.z * speed;


     if (m_CharacterController.isGrounded)
     {
         m_MoveDir.y = -m_StickToGroundForce;

         if (m_Jump)
         {
             m_MoveDir.y = m_JumpSpeed;
             PlayJumpSound();
             m_Jump = false;
             m_Jumping = true;
         }
     }
     else
     {
         m_MoveDir += Physics.gravity * m_GravityMultiplier * Time.fixedDeltaTime;
     }
     m_CollisionFlags = m_CharacterController.Move(m_MoveDir * Time.fixedDeltaTime);

     ProgressStepCycle(speed);

 }


 private void PlayJumpSound()
 {
     m_AudioSource.clip = m_JumpSound;
     m_AudioSource.Play();
 }


 private void ProgressStepCycle(float speed)
 {
     if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
     {
         m_StepCycle += (m_CharacterController.velocity.magnitude + (speed * (m_IsWalking ? 1f : m_RunstepLenghten))) *
                      Time.fixedDeltaTime;
     }

     if (!(m_StepCycle > m_NextStep))
     {
         return;
     }

     m_NextStep = m_StepCycle + m_StepInterval;

     PlayFootStepAudio();
 }


 private void PlayFootStepAudio()
 {
     if (!m_CharacterController.isGrounded)
     {
         return;
     }
     // pick & play a random footstep sound from the array,
     // excluding sound at index 0
     int n = Random.Range(1, m_FootstepSounds.Length);
     m_AudioSource.clip = m_FootstepSounds[n];
     m_AudioSource.PlayOneShot(m_AudioSource.clip);
     // move picked sound to index 0 so it's not picked next time
     m_FootstepSounds[n] = m_FootstepSounds[0];
     m_FootstepSounds[0] = m_AudioSource.clip;
 }


 private void UpdateCameraPosition(float speed)
 {


 }


 private void GetInput(out float speed)
 {
     // Read input
     float horizontal = Input.GetAxis("Horizontal");
     float vertical = Input.GetAxis("Vertical");

     bool waswalking = m_IsWalking;

     {
         

     }

if !MOBILE_INPUT

    // On standalone builds, walk/run speed is modified by a key press.
     // keep track of whether or not the character is walking or running
     m_IsWalking = !Input.GetKey(KeyCode.LeftShift);

endif

    // set the desired speed to be walking or running
     speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
     m_Input = new Vector2(horizontal, vertical);

     // normalize input if it exceeds 1 in combined length:
     if (m_Input.sqrMagnitude > 1)
     {
         m_Input.Normalize();
     }

     // handle speed change to give an fov kick
     // only if the player is going to a run, is running and the fovkick is to be used
     if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
     {
         StopAllCoroutines();
         //    StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
     }
 }


 private void RotateView()
 {
     m_MouseLook.LookRotation(transform, m_Camera.transform);
 
 }

 

 


     
 }

 
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

162 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

Related Questions

animation script behaving differently after build and run on a different computer. 1 Answer

A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.GetComponent()' 0 Answers

How do I get rid of these scripting Errors? 2 Answers

Having problems with manually animating the Animator, some frames happen twice 0 Answers

I am confused on triggering animations by key press. 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