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 /
avatar image
0
Question by JakMesh · Oct 03, 2015 at 10:46 PM · unity 5scripting problemrunning

Characterdoesn't run on Aiming mode

i hope i can find a help from anyone as soon as possible.

i have this assets that im working from it, in other project, and i wants the cherecter to be able to run while in Aiming mode, but seem it doesn't work, i tried to add some function to it, but also it did not work, i hope i can find someone to help me figure it out.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControl : MonoBehaviour
 {
 
     public float walkSpeed = 0.15f;
     public float runSpeed = 1.0f;
     public float sprintSpeed = 2.0f;
     public float flySpeed = 4.0f;
 
     public float turnSmoothing = 3.0f;
     public float aimTurnSmoothing = 15.0f;
     public float speedDampTime = 0.1f;
 
     public float jumpHeight = 5.0f;
     public float jumpCooldown = 1.0f;
 
     private float timeToNextJump = 0;
     
     private float speed;
 
     private Vector3 lastDirection;
 
     private Animator anim;
     private int speedFloat;
     private int jumpBool;
     private int hFloat;
     private int vFloat;
     private int aimBool;
     private int flyBool;
     private int groundedBool;
     private Transform cameraTransform;
 
     private float h;
     private float v;
 
     private bool aim;
 
     private bool run;
     private bool sprint;
 
     private bool isMoving;
 
     // fly
     private bool fly = false;
     private float distToGround;
     private float sprintFactor;
 
     void Awake()
     {
         anim = GetComponent<Animator> ();
         cameraTransform = Camera.main.transform;
 
         speedFloat = Animator.StringToHash("Speed");
         jumpBool = Animator.StringToHash("Jump");
         hFloat = Animator.StringToHash("H");
         vFloat = Animator.StringToHash("V");
         aimBool = Animator.StringToHash("Aim");
         // fly
         flyBool = Animator.StringToHash ("Fly");
         groundedBool = Animator.StringToHash("Grounded");
         distToGround = GetComponent<Collider>().bounds.extents.y;
         sprintFactor = sprintSpeed / runSpeed;
     }
 
     bool IsGrounded() {
         return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
     }
 
     void Update()
     {
         // fly
         if(Input.GetButtonDown ("Fly"))
             fly = !fly;
         aim = Input.GetButton("Aim");
         h = Input.GetAxis("Horizontal");
         v = Input.GetAxis("Vertical");
         run = Input.GetButton ("Run");
         sprint = Input.GetButton ("Sprint");
         isMoving = Mathf.Abs(h) > 0.1 || Mathf.Abs(v) > 0.1;
     }
 
     void FixedUpdate()
     {
         anim.SetBool (aimBool, IsAiming());
         anim.SetFloat(hFloat, h);
         anim.SetFloat(vFloat, v);
         
         // Fly
         anim.SetBool (flyBool, fly);
         GetComponent<Rigidbody>().useGravity = !fly;
         anim.SetBool (groundedBool, IsGrounded ());
         if(fly)
             FlyManagement(h,v);
 
         else
         {
             MovementManagement (h, v, run, sprint);
             JumpManagement ();
         }
     }
 
     // fly
     void FlyManagement(float horizontal, float vertical)
     {
         Vector3 direction = Rotating(horizontal, vertical);
         GetComponent<Rigidbody>().AddForce(direction * flySpeed * 100 * (sprint?sprintFactor:1));
     }
 
     void JumpManagement()
     {
         if (GetComponent<Rigidbody>().velocity.y < 10) // already jumped
         {
             anim.SetBool (jumpBool, false);
             if(timeToNextJump > 0)
                 timeToNextJump -= Time.deltaTime;
         }
         if (Input.GetButtonDown ("Jump"))
         {
             anim.SetBool(jumpBool, true);
             if(speed > 0 && timeToNextJump <= 0 && !aim)
             {
                 GetComponent<Rigidbody>().velocity = new Vector3(0, jumpHeight, 0);
                 timeToNextJump = jumpCooldown;
             }
         }
     }
 
     void MovementManagement(float horizontal, float vertical, bool running, bool sprinting)
     {
         Rotating(horizontal, vertical);
 
         if(isMoving)
         {
             if(sprinting)
             {
                 speed = sprintSpeed;
             }
             else if (running)
             {
                 speed = runSpeed;
             }
             else
             {
                 speed = walkSpeed;
             }
 
             anim.SetFloat(speedFloat, speed, speedDampTime, Time.deltaTime);
         }
         else
         {
             speed = 0f;
             anim.SetFloat(speedFloat, 0f);
         }
         GetComponent<Rigidbody>().AddForce(Vector3.forward*speed);
     }
 
     Vector3 Rotating(float horizontal, float vertical)
     {
         Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
         if (!fly)
             forward.y = 0.0f;
         forward = forward.normalized;
 
         Vector3 right = new Vector3(forward.z, 0, -forward.x);
 
         Vector3 targetDirection;
 
         float finalTurnSmoothing;
 
         if(IsAiming())
         {
             targetDirection = forward;
             finalTurnSmoothing = aimTurnSmoothing;
         }
         else
         {
             targetDirection = forward * vertical + right * horizontal;
             finalTurnSmoothing = turnSmoothing;
         }
 
         if((isMoving && targetDirection != Vector3.zero) || IsAiming())
         {
             Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);
             // fly
             if (fly)
                 targetRotation *= Quaternion.Euler (90, 0, 0);
 
             Quaternion newRotation = Quaternion.Slerp(GetComponent<Rigidbody>().rotation, targetRotation, finalTurnSmoothing * Time.deltaTime);
             GetComponent<Rigidbody>().MoveRotation (newRotation);
             lastDirection = targetDirection;
         }
         //idle - fly or grounded
         if(!(Mathf.Abs(h) > 0.9 || Mathf.Abs(v) > 0.9))
         {
             Repositioning();
         }
 
         return targetDirection;
     }    
 
     private void Repositioning()
     {
         Vector3 repositioning = lastDirection;
         if(repositioning != Vector3.zero)
         {
             repositioning.y = 0;
             Quaternion targetRotation = Quaternion.LookRotation (repositioning, Vector3.up);
             Quaternion newRotation = Quaternion.Slerp(GetComponent<Rigidbody>().rotation, targetRotation, turnSmoothing * Time.deltaTime);
             GetComponent<Rigidbody>().MoveRotation (newRotation);
         }
     }
 
     public bool IsFlying()
     {
         return fly;
     }
 
     public bool IsAiming()
     {
         return aim && !fly;
     }
 
     public bool isSprinting()
     {
         return sprint && !aim && (isMoving);
     }
 }
 
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object Plane.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/Plane.cs:37) 0 Answers

enable and disable script on Unity gameobject 0 Answers

[Help!]Unity FPS Health script error. 1 Answer

Sprite image not changing 0 Answers

Animation Loop 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