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 /
avatar image
0
Question by JuanseCoello · Jul 06, 2013 at 05:11 AM · charactercontrollerthird-personmotor

Help to understand script (Ninja character)

I am new to scripting, but i learn fast. I ask for patience, because i am a noob. I understand many things about the script, but only the general overview.

The script moves the character along the mouse movement, I want it to be still and move the character with the keyboard while the camera is still. Like in tomb raider. By the way the script needs a capsule collider and a rigid body to work.

I want to understand more about the script and all of its components. Just give me your best opinion of what you understand please, and i will put the pieces together. :) Here is the script, is a bit long:

using UnityEngine; using System.Collections;

public delegate void JumpDelegate (); public delegate void BalanceDelegate (bool b); public delegate void CombatDelegate (); public delegate void EvadeDelegate (string s); public delegate void SneakDelegate (bool b);

[RequireComponent(typeof(Rigidbody))] [RequireComponent(typeof(CapsuleCollider))]

public class HeroMotor : MonoBehaviour { public bool canMove = true;

 public bool canStopFast = true;
 
 public LayerMask groundLayers = -1;
 public float speed = 0.7f;
 public float sideSpeed = 0.6f;
 public float walkSpeed = 0.31f;
 public float sprintSpeed = 1.2f;
 public float mouseRotSpeed = 1.0f;
 public float jumpPower = 1.0f;
 public float groundDrag = 6.5f;
 public float airDrag = 0.0f;
 
 public float groundedOffsetRay = -0.1f;
 const float groundedDistance = 0.5f;
 const float inputThreshold = 0.01f;
 
 
 public JumpDelegate doJumpDel = null;
 public BalanceDelegate doBalanceDel = null;
 public CombatDelegate doCombatDel = null;
 public CombatDelegate doSwitchWeapDel = null;
 public EvadeDelegate doEvadeDel = null;
 public SneakDelegate doSneakDel = null;
 
 Rigidbody rb;
 Transform hero;
 HeroClimb hClimb;
 HeroAnim hAnim;
 HeroPhysic hPhys;
 
 public enum MoveState
 {
     Normal,
     Evade,
     Walking,
     Sprinting,
     Sneaking,
     Balancing,
     CombatStance
 }
 public MoveState moveState = MoveState.Normal;
 
 bool grounded;
 public bool Grounded { get { return grounded; } }
 
 // Double tap
 Vector3 evadeDir;
 public float tapSpeed = 0.3f; // Time between the taps
 float lastTapTime = 0.0f;
 
 bool isDoubleTap = false;
 string lastKey = " ";
 
 float curSpeed;
 bool getsSpeed = false;
 
 public bool isAfterTap = true;
 bool isAfterStop = true;
 bool canJump = true;
 
 //=================================================================================================================o
 void Start ()
 {
     rb = rigidbody;
     hero = transform;
     rb.freezeRotation = true;
     
     hClimb = GetComponent <HeroClimb> () as HeroClimb;
     hAnim = GetComponent <HeroAnim> () as HeroAnim;
     hPhys = GetComponent <HeroPhysic> () as HeroPhysic;
     
     hAnim.doSpeedDel += DoSetSpeed;
     hAnim.doSwitchDel += DoSwitch;
     hPhys.doPhysDel += DoReset;
     hClimb.doClimbDel += DoReset;
 }
 //=================================================================================================================o
 void DoSetSpeed (float f)
 {
     curSpeed = f;
     getsSpeed = true;
 }
 //=================================================================================================================o
 void DoSwitch (bool b)
 {
     isDoubleTap = b;
     
     // start a fresh cooldown
     isAfterTap = false;
     StartCoroutine (DTapCoolDown(0.2f)); // Normal modus
 }
 //=================================================================================================================o
 void DoReset (string s)
 {
     // Ragdoll modus ends here
     if (s == "End" || s == "None" || s == "Edge" || s == "Ledge")
     {
         //Reset
         moveState = MoveState.Normal;
         if (moveState == MoveState.Sneaking)
         {
             if (doSneakDel != null) { doSneakDel (true); }
         }
     }
 }
 //=================================================================================================================o
 void DoubleTap ()
 {
     // Double Tap WASD
     if (Input.GetKeyDown (KeyCode.W) || Input.GetKeyDown (KeyCode.S)
         || Input.GetKeyDown (KeyCode.A) || Input.GetKeyDown (KeyCode.D))
     {
         if ((Time.time - lastTapTime) < tapSpeed)
         {
             if (Input.GetAxis ("Vertical") < 0.0f)
             {
                 evadeDir = -hero.forward;
                 lastKey = "S";
             }
             else if (Input.GetAxis ("Vertical") > 0.0f)
             {
                 evadeDir = hero.forward;
                 lastKey = "W";
             }
             else if (Input.GetAxis ("Horizontal") < 0.0f)
             {
                 evadeDir = -hero.right;
                 lastKey = "A";
             }
             else if (Input.GetAxis ("Horizontal") > 0.0f)
             {
                 evadeDir = hero.right;
                 lastKey = "D";
             }
             
             if (doEvadeDel != null) { doEvadeDel (lastKey); }
             
             isDoubleTap = true;
             moveState = MoveState.Evade;
             isAfterTap = false;
         }
         lastTapTime = Time.time;
     }
 }
 //=================================================================================================================o
 void Update ()
 {
     // MAIN INPUT
     if (canMove && !isDoubleTap) 
     {
         // Stop Rotation if Left Shift is pressed
         if (!Input.GetKey (KeyCode.LeftShift)) 
         {
             float rotato = Input.GetAxis ("Mouse X") * mouseRotSpeed * Time.deltaTime;
             hero.RotateAround (hero.up, rotato);
             
             DoubleTap ();
         }
         
         // Switch states if not balancing
         if (moveState != MoveState.Balancing)
         {
             // Left Shift to toggle sprint
             if (Input.GetKeyDown (KeyCode.LeftShift) && moveState != MoveState.Sneaking && Grounded)
             {
                 moveState = moveState != MoveState.Sprinting ? MoveState.Sprinting : MoveState.Normal;
             }
             
             // Switch Walk/Run with X
             else if (Input.GetKeyDown (KeyCode.X)) 
             {
                 // Walking if not already else Normal
                 moveState = moveState != MoveState.Walking ? MoveState.Walking : MoveState.Normal;
             }
             
             // Sneak mode
             else if (Input.GetKeyDown (KeyCode.V)) 
             {
                 // Sneaking if not already else Normal
                 moveState = moveState != MoveState.Sneaking ? MoveState.Sneaking : MoveState.Normal;
                 
                 bool b = moveState == MoveState.Sneaking ? true : false;
                 // Sneak state delegate
                 if (doSneakDel != null) { doSneakDel (b); }
             }
             
             // Switch Combat mode if not in "Cooldown"
             else if (Input.GetKeyDown (KeyCode.C))
             {
                 // Combat stance if not already else Normal
                 if (hAnim.mainState != HeroAnim.MainState.Combat)
                     moveState = MoveState.CombatStance;
                 else
                     moveState = MoveState.Normal;
                 
                 // Combat state delegate
                 if (doCombatDel != null) { doCombatDel(); }
             }
             else if (Input.GetKeyDown(KeyCode.Q))
             {
                 if (hAnim.mainState != HeroAnim.MainState.Combat && !hAnim.isWeaponDraw)
                 {
                     // cycle through weapon states
                     hAnim.weaponState++; // Next
                     if (hAnim.weaponState > HeroAnim.WeaponState.None) // Last in the enum
                         hAnim.weaponState = HeroAnim.WeaponState.Unarmed; // Start at the first
                     
                     // WeaponSwitch state delegate
                     if (doSwitchWeapDel != null) { doSwitchWeapDel(); }
                 }
             }
         }
     }
 }
 //=================================================================================================================o
 
 // Forward movement vector for moving down stronger slopes
 Vector3 ForwardVec (RaycastHit hit)
 {
     float angl = Vector3.Angle(hero.forward, hit.normal);
     if (angl < 68f) // 90 is flat ground
     {
         Vector3 slopeV = hero.forward - hero.up;
         return slopeV;
     }
     else // Move straight 
         return hero.forward;
 }
 //=================================================================================================================o
 void FixedUpdate ()
 {
     RaycastHit hit;
     grounded = Physics.Raycast (hero.position + hero.up * -groundedOffsetRay,
         hero.up * -1, out hit, groundedDistance, groundLayers);
     
     if (grounded && canMove)
     {
         // If any Double tap
         if (!isDoubleTap) 
         {
             // Horizontal / Vertical velocity
             Vector3 curVelocity = Input.GetAxis ("Vertical") * ForwardVec(hit)
                 + Input.GetAxis ("Horizontal") * hero.right;
             // If not receiving speed via delegate
             if (!getsSpeed)
             {
                 // Jump
                 if (Input.GetButton ("Jump") && canJump)
                 {
                     rb.AddForce (jumpPower * hero.up + 
                         rb.velocity.normalized /2.5f, ForceMode.VelocityChange);
                     
                     // Jump delegate
                     if (doJumpDel != null) { doJumpDel(); }
                     
                     // Start cooldown until we can jump again
                     StartCoroutine (JumpCoolDown(0.7f));
                 }
                 
                 // Walk, sprint and sneak speed
                 else if (moveState == MoveState.Walking || moveState == MoveState.Sneaking)
                     curSpeed = walkSpeed;
                 else if (moveState == MoveState.Sprinting)
                     curSpeed = sprintSpeed;
                 else
                     curSpeed = speed;
                 
                 // Back-Side speed
                 if (Input.GetAxis ("Vertical") < 0.0f)
                 {
                     curSpeed = walkSpeed;
                 }
                 else if (Input.GetAxis ("Horizontal") != 0.0f && moveState == MoveState.Normal)
                         curSpeed = sideSpeed;
             }
             
             // Apply movement if descent input and not double tab
             if (curVelocity.magnitude > inputThreshold)
             {
                 rb.AddForce (curVelocity.normalized * curSpeed, ForceMode.VelocityChange);
                 
                 // Stop "anti slide" timed trigger
                 if (Input.GetKeyUp (KeyCode.W) && canStopFast)
                 {
                     isAfterStop = false;
                     StartCoroutine (StopCoolDown(0.7f));
                 }
             }
             
             else // Don't slide if not jumping and not in evade modus
             {
                 if (!Input.GetButton("Jump") && isAfterTap && isAfterStop)
                 {
                     rb.velocity = new Vector3 (0.0f, rb.velocity.y, 0.0f);
                 }
             }
             
             // Balancing on climb collider, layer 9
             if (moveState != MoveState.Balancing)
             {
                 if (hit.transform && hit.transform.gameObject.layer == 9) // Hit climb-layer collider
                 {
                     if (doBalanceDel != null) { doBalanceDel(true); }
                     moveState = MoveState.Balancing;
                 }
             }
             else // Normal mode
             {
                 if (hit.transform && hit.transform.gameObject.layer != 9) // Hit other-layer collider
                 {
                     if (doBalanceDel != null) { doBalanceDel(false); }
                     // Back to normal
                     moveState = MoveState.Normal;
                 }
             }
         }
         else // Situaltional velocity ( Double tap )
         {
             if (isDoubleTap)
             {
                 rb.AddForce (evadeDir.normalized * 0.7f/*curSpeed*/, ForceMode.VelocityChange);
             }
             else // Don't move horizontal
             {
                 rb.velocity = new Vector3 (0.0f, rb.velocity.y, 0.0f);
             }
         }
         rb.drag = groundDrag;
     }
     else // In air
     {
         rb.drag = airDrag;
     }

     if (!Input.GetButton("Fire1")) // If not shooting, left mouse
     {
         getsSpeed = false; // Is not receiving speed float from HeroAnim --> Back to normal
     }
 }
 
 // Double tap cool-down
 IEnumerator DTapCoolDown (float sec)
 {
     yield return new WaitForSeconds (sec);
     if (hAnim.mainState != HeroAnim.MainState.Combat)
         moveState = MoveState.Normal;
     else
         moveState = MoveState.CombatStance;
     isAfterTap = true;
 }
 // Stop cool-down
 IEnumerator StopCoolDown (float sec)
 {
     yield return new WaitForSeconds (sec);
     isAfterStop = true;
 }
 // Double tap cool-down
 IEnumerator JumpCoolDown (float sec)
 {
     canJump = true;
     yield return new WaitForSeconds (sec);
     canJump = false;
     yield return new WaitForSeconds (sec);
     canJump = true;
 }

}

Comment
Add comment · Show 1
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
avatar image Lo0NuhtiK · Jul 06, 2013 at 05:13 AM 1
Share

Here... ---throws grappling hook down--- ...had to borrow that from the ninja so I could get down that wall of code.

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Character Controller Gravity 1 Answer

Unity Build Crashes Once Characters Touches Ground 0 Answers

Help with Colliders? 0 Answers

Character Flying around uncontrollably 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