Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Getting float from other script on same GameObject

Discussion in 'Scripting' started by D42mon, Sep 16, 2021.

  1. D42mon

    D42mon

    Joined:
    Jul 16, 2021
    Posts:
    12
    Ok, I give up, I am on this for almost 7 hours now, it is 2am, I can't stand it, I read the entire Internet to find a solution, I tried everything and can't come up with a solution that works.

    I have a player GameObject. Two scripts attached:
    1 - vThirdPersonController.cs
    2 - RollAndDash.cs

    I try to get the public float runningSpeed from V Third Person Controller to be modified in Roll And Dash.

    Here is my code in RollAndDash.cs:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RollAndDash : MonoBehaviour
    6. {
    7.  
    8.     public GameObject player;
    9.     Animator m_Animator;
    10.  
    11.  
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         m_Animator = player.GetComponent<Animator>();
    17.  
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (Input.GetKeyDown(KeyCode.LeftControl) && !Input.GetKeyDown(KeyCode.Space) && m_Animator.GetBool("IsGrounded") == true)
    24.         {
    25.                
    26.             player.GetComponent<Animator>().SetFloat("IsRolling", 1);
    27.             m_Animator.Play("IsRolling");
    28.             player.GetComponent<VThirdPersonController>().runningSpeed = 150f;
    29.  
    30.         }
    31.  
    32.        
    33.     }
    34.  
    35.  
    36. }
    37.  
    Whatever I do, I allways get this line wrong: player.GetComponent<VThirdPersonController>().runningSpeed = 150f;

    I am totally lost, any help would be gladly appreciated!

    Here are some screenshots to help:


     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    Perhaps because it is actually
    vThirdPersonController
    and not
    VThirdPersonController
    ?
    But what do you mean by "I always get this line wrong"? Is it throwing an error? If so, give us the exact error. Perhaps you could also show us the vThirdPersonController script so we can check the class name, etc.
     
  3. D42mon

    D42mon

    Joined:
    Jul 16, 2021
    Posts:
    12
    Hello and thanks for answering. In fact, even with a lower case "v", it doesn't work.

    The thing is that the float I am looking for is not even in the vThirdPersonController.cs script but in the vThirdPersonMotor.cs script. However, it does appear in the vThirdPersonController component. Let me show you the code for both of these codes:

    vThirdPersonController.cs :
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Invector.vCharacterController
    4. {
    5.     public class vThirdPersonController : vThirdPersonAnimator
    6.     {
    7.         public virtual void ControlAnimatorRootMotion()
    8.         {
    9.             if (!this.enabled) return;
    10.  
    11.             if (inputSmooth == Vector3.zero)
    12.             {
    13.                 transform.position = animator.rootPosition;
    14.                 transform.rotation = animator.rootRotation;
    15.             }
    16.  
    17.             if (useRootMotion)
    18.                 MoveCharacter(moveDirection);
    19.         }
    20.  
    21.         public virtual void ControlLocomotionType()
    22.         {
    23.             if (lockMovement) return;
    24.  
    25.             if (locomotionType.Equals(LocomotionType.FreeWithStrafe) && !isStrafing || locomotionType.Equals(LocomotionType.OnlyFree))
    26.             {
    27.                 SetControllerMoveSpeed(freeSpeed);
    28.                 SetAnimatorMoveSpeed(freeSpeed);
    29.             }
    30.             else if (locomotionType.Equals(LocomotionType.OnlyStrafe) || locomotionType.Equals(LocomotionType.FreeWithStrafe) && isStrafing)
    31.             {
    32.                 isStrafing = true;
    33.                 SetControllerMoveSpeed(strafeSpeed);
    34.                 SetAnimatorMoveSpeed(strafeSpeed);
    35.             }
    36.  
    37.             if (!useRootMotion)
    38.                 MoveCharacter(moveDirection);
    39.         }
    40.  
    41.         public virtual void ControlRotationType()
    42.         {
    43.             if (lockRotation) return;
    44.  
    45.             bool validInput = input != Vector3.zero || (isStrafing ? strafeSpeed.rotateWithCamera : freeSpeed.rotateWithCamera);
    46.  
    47.             if (validInput)
    48.             {
    49.                 // calculate input smooth
    50.                 inputSmooth = Vector3.Lerp(inputSmooth, input, (isStrafing ? strafeSpeed.movementSmooth : freeSpeed.movementSmooth) * Time.deltaTime);
    51.  
    52.                 Vector3 dir = (isStrafing && (!isSprinting || sprintOnlyFree == false) || (freeSpeed.rotateWithCamera && input == Vector3.zero)) && rotateTarget ? rotateTarget.forward : moveDirection;
    53.                 RotateToDirection(dir);
    54.             }
    55.         }
    56.  
    57.         public virtual void UpdateMoveDirection(Transform referenceTransform = null)
    58.         {
    59.             if (input.magnitude <= 0.01)
    60.             {
    61.                 moveDirection = Vector3.Lerp(moveDirection, Vector3.zero, (isStrafing ? strafeSpeed.movementSmooth : freeSpeed.movementSmooth) * Time.deltaTime);
    62.                 return;
    63.             }
    64.  
    65.             if (referenceTransform && !rotateByWorld)
    66.             {
    67.                 //get the right-facing direction of the referenceTransform
    68.                 var right = referenceTransform.right;
    69.                 right.y = 0;
    70.                 //get the forward direction relative to referenceTransform Right
    71.                 var forward = Quaternion.AngleAxis(-90, Vector3.up) * right;
    72.                 // determine the direction the player will face based on input and the referenceTransform's right and forward directions
    73.                 moveDirection = (inputSmooth.x * right) + (inputSmooth.z * forward);
    74.             }
    75.             else
    76.             {
    77.                 moveDirection = new Vector3(inputSmooth.x, 0, inputSmooth.z);
    78.             }
    79.         }
    80.  
    81.         public virtual void Sprint(bool value)
    82.         {
    83.             var sprintConditions = (input.sqrMagnitude > 0.1f && isGrounded &&
    84.                 !(isStrafing && !strafeSpeed.walkByDefault && (horizontalSpeed >= 0.5 || horizontalSpeed <= -0.5 || verticalSpeed <= 0.1f)));
    85.  
    86.             if (value && sprintConditions)
    87.             {
    88.                 if (input.sqrMagnitude > 0.1f)
    89.                 {
    90.                     if (isGrounded && useContinuousSprint)
    91.                     {
    92.                         isSprinting = !isSprinting;
    93.                     }
    94.                     else if (!isSprinting)
    95.                     {
    96.                         isSprinting = true;
    97.                     }
    98.                 }
    99.                 else if (!useContinuousSprint && isSprinting)
    100.                 {
    101.                     isSprinting = false;
    102.                 }
    103.             }
    104.             else if (isSprinting)
    105.             {
    106.                 isSprinting = false;
    107.             }
    108.         }
    109.  
    110.         public virtual void Strafe()
    111.         {
    112.             isStrafing = !isStrafing;
    113.         }
    114.  
    115.         public virtual void Jump()
    116.         {
    117.             // trigger jump behaviour
    118.             jumpCounter = jumpTimer;
    119.             isJumping = true;
    120.  
    121.             // trigger jump animations
    122.             if (input.sqrMagnitude < 0.1f)
    123.                 animator.CrossFadeInFixedTime("Jump", 0.1f);
    124.             else
    125.                 animator.CrossFadeInFixedTime("JumpMove", .2f);
    126.         }
    127.        
    128.         public virtual void Tele()
    129.         {
    130.             if (Input.GetMouseButtonDown(0))
    131.             {
    132.                 isTele = true;
    133.             }
    134.         }
    135.     }
    136. }
    vThirdPersonMotor.cs :
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Invector.vCharacterController
    4. {
    5.     public class vThirdPersonMotor : MonoBehaviour
    6.     {
    7.         #region Inspector Variables
    8.  
    9.         [Header("- Movement")]
    10.  
    11.         [Tooltip("Turn off if you have 'in place' animations and use this values above to move the character, or use with root motion as extra speed")]
    12.         public bool useRootMotion = false;
    13.         [Tooltip("Use this to rotate the character using the World axis, or false to use the camera axis - CHECK for Isometric Camera")]
    14.         public bool rotateByWorld = false;
    15.         [Tooltip("Check This to use sprint on press button to your Character run until the stamina finish or movement stops\nIf uncheck your Character will sprint as long as the SprintInput is pressed or the stamina finishes")]
    16.         public bool useContinuousSprint = true;
    17.         [Tooltip("Check this to sprint always in free movement")]
    18.         public bool sprintOnlyFree = true;
    19.         public enum LocomotionType
    20.         {
    21.             FreeWithStrafe,
    22.             OnlyStrafe,
    23.             OnlyFree,
    24.         }
    25.         public LocomotionType locomotionType = LocomotionType.FreeWithStrafe;
    26.  
    27.         public vMovementSpeed freeSpeed, strafeSpeed;
    28.  
    29.         [Header("- Airborne")]
    30.  
    31.         [Tooltip("Use the currently Rigidbody Velocity to influence on the Jump Distance")]
    32.         public bool jumpWithRigidbodyForce = false;
    33.         [Tooltip("Rotate or not while airborne")]
    34.         public bool jumpAndRotate = true;
    35.         [Tooltip("How much time the character will be jumping")]
    36.         public float jumpTimer = 0.3f;
    37.         [Tooltip("Add Extra jump height, if you want to jump only with Root Motion leave the value with 0.")]
    38.         public float jumpHeight = 4f;
    39.  
    40.         [Tooltip("Speed that the character will move while airborne")]
    41.         public float airSpeed = 5f;
    42.         [Tooltip("Smoothness of the direction while airborne")]
    43.         public float airSmooth = 6f;
    44.         [Tooltip("Apply extra gravity when the character is not grounded")]
    45.         public float extraGravity = -10f;
    46.         [HideInInspector]
    47.         public float limitFallVelocity = -15f;
    48.  
    49.         [Header("- Ground")]
    50.         [Tooltip("Layers that the character can walk on")]
    51.         public LayerMask groundLayer = 1 << 0;
    52.         [Tooltip("Distance to became not grounded")]
    53.         public float groundMinDistance = 0.25f;
    54.         public float groundMaxDistance = 0.5f;
    55.         [Tooltip("Max angle to walk")]
    56.         [Range(30, 80)] public float slopeLimit = 75f;
    57.         #endregion
    58.  
    59.         #region Components
    60.  
    61.         internal Animator animator;
    62.         internal Rigidbody _rigidbody;                                                      // access the Rigidbody component
    63.         internal PhysicMaterial frictionPhysics, maxFrictionPhysics, slippyPhysics;         // create PhysicMaterial for the Rigidbody
    64.         internal CapsuleCollider _capsuleCollider;                                          // access CapsuleCollider information
    65.  
    66.         #endregion
    67.  
    68.         #region Internal Variables
    69.  
    70.         // movement bools
    71.         internal bool isJumping;
    72.         internal bool isTele;
    73.         internal bool isStrafing
    74.         {
    75.             get
    76.             {
    77.                 return _isStrafing;
    78.             }
    79.             set
    80.             {
    81.                 _isStrafing = value;
    82.             }
    83.         }
    84.         internal bool isGrounded { get; set; }
    85.         internal bool isSprinting { get; set; }
    86.         public bool stopMove { get; protected set; }
    87.  
    88.         internal float inputMagnitude;                      // sets the inputMagnitude to update the animations in the animator controller
    89.         internal float verticalSpeed;                       // set the verticalSpeed based on the verticalInput
    90.         internal float horizontalSpeed;                     // set the horizontalSpeed based on the horizontalInput      
    91.         internal float moveSpeed;                           // set the current moveSpeed for the MoveCharacter method
    92.         internal float verticalVelocity;                    // set the vertical velocity of the rigidbody
    93.         internal float colliderRadius, colliderHeight;      // storage capsule collider extra information      
    94.         internal float heightReached;                       // max height that character reached in air;
    95.         internal float jumpCounter;                         // used to count the routine to reset the jump
    96.         internal float groundDistance;                      // used to know the distance from the ground
    97.         internal RaycastHit groundHit;                      // raycast to hit the ground
    98.         internal bool lockMovement = false;                 // lock the movement of the controller (not the animation)
    99.         internal bool lockRotation = false;                 // lock the rotation of the controller (not the animation)      
    100.         internal bool _isStrafing;                          // internally used to set the strafe movement              
    101.         internal Transform rotateTarget;                    // used as a generic reference for the camera.transform
    102.         internal Vector3 input;                             // generate raw input for the controller
    103.         internal Vector3 colliderCenter;                    // storage the center of the capsule collider info              
    104.         internal Vector3 inputSmooth;                       // generate smooth input based on the inputSmooth value      
    105.         internal Vector3 moveDirection;                     // used to know the direction you're moving
    106.  
    107.         #endregion
    108.  
    109.         public void Init()
    110.         {
    111.             animator = GetComponent<Animator>();
    112.             animator.updateMode = AnimatorUpdateMode.AnimatePhysics;
    113.  
    114.             // slides the character through walls and edges
    115.             frictionPhysics = new PhysicMaterial();
    116.             frictionPhysics.name = "frictionPhysics";
    117.             frictionPhysics.staticFriction = .25f;
    118.             frictionPhysics.dynamicFriction = .25f;
    119.             frictionPhysics.frictionCombine = PhysicMaterialCombine.Multiply;
    120.  
    121.             // prevents the collider from slipping on ramps
    122.             maxFrictionPhysics = new PhysicMaterial();
    123.             maxFrictionPhysics.name = "maxFrictionPhysics";
    124.             maxFrictionPhysics.staticFriction = 1f;
    125.             maxFrictionPhysics.dynamicFriction = 1f;
    126.             maxFrictionPhysics.frictionCombine = PhysicMaterialCombine.Maximum;
    127.  
    128.             // air physics
    129.             slippyPhysics = new PhysicMaterial();
    130.             slippyPhysics.name = "slippyPhysics";
    131.             slippyPhysics.staticFriction = 0f;
    132.             slippyPhysics.dynamicFriction = 0f;
    133.             slippyPhysics.frictionCombine = PhysicMaterialCombine.Minimum;
    134.  
    135.             // rigidbody info
    136.             _rigidbody = GetComponent<Rigidbody>();
    137.  
    138.             // capsule collider info
    139.             _capsuleCollider = GetComponent<CapsuleCollider>();
    140.  
    141.             // save your collider preferences
    142.             colliderCenter = GetComponent<CapsuleCollider>().center;
    143.             colliderRadius = GetComponent<CapsuleCollider>().radius;
    144.             colliderHeight = GetComponent<CapsuleCollider>().height;
    145.  
    146.             isGrounded = true;
    147.         }
    148.  
    149.         public virtual void UpdateMotor()
    150.         {
    151.             CheckGround();
    152.             CheckSlopeLimit();
    153.             ControlJumpBehaviour();
    154.             AirControl();
    155.         }
    156.  
    157.         #region Locomotion
    158.  
    159.         public virtual void SetControllerMoveSpeed(vMovementSpeed speed)
    160.         {
    161.             if (speed.walkByDefault)
    162.                 moveSpeed = Mathf.Lerp(moveSpeed, isSprinting ? speed.runningSpeed : speed.walkSpeed, speed.movementSmooth * Time.deltaTime);
    163.             else
    164.                 moveSpeed = Mathf.Lerp(moveSpeed, isSprinting ? speed.sprintSpeed : speed.runningSpeed, speed.movementSmooth * Time.deltaTime);
    165.         }
    166.  
    167.         public virtual void MoveCharacter(Vector3 _direction)
    168.         {
    169.             // calculate input smooth
    170.             inputSmooth = Vector3.Lerp(inputSmooth, input, (isStrafing ? strafeSpeed.movementSmooth : freeSpeed.movementSmooth) * Time.deltaTime);
    171.  
    172.             if (!isGrounded || isJumping) return;
    173.  
    174.             _direction.y = 0;
    175.             _direction.x = Mathf.Clamp(_direction.x, -1f, 1f);
    176.             _direction.z = Mathf.Clamp(_direction.z, -1f, 1f);
    177.             // limit the input
    178.             if (_direction.magnitude > 1f)
    179.                 _direction.Normalize();
    180.  
    181.             Vector3 targetPosition = (useRootMotion ? animator.rootPosition : _rigidbody.position) + _direction * (stopMove ? 0 : moveSpeed) * Time.deltaTime;
    182.             Vector3 targetVelocity = (targetPosition - transform.position) / Time.deltaTime;
    183.  
    184.             bool useVerticalVelocity = true;
    185.             if (useVerticalVelocity) targetVelocity.y = _rigidbody.velocity.y;
    186.             _rigidbody.velocity = targetVelocity;
    187.         }
    188.  
    189.         public virtual void CheckSlopeLimit()
    190.         {
    191.             if (input.sqrMagnitude < 0.1) return;
    192.  
    193.             RaycastHit hitinfo;
    194.             var hitAngle = 0f;
    195.  
    196.             if (Physics.Linecast(transform.position + Vector3.up * (_capsuleCollider.height * 0.5f), transform.position + moveDirection.normalized * (_capsuleCollider.radius + 0.2f), out hitinfo, groundLayer))
    197.             {
    198.                 hitAngle = Vector3.Angle(Vector3.up, hitinfo.normal);
    199.  
    200.                 var targetPoint = hitinfo.point + moveDirection.normalized * _capsuleCollider.radius;
    201.                 if ((hitAngle > slopeLimit) && Physics.Linecast(transform.position + Vector3.up * (_capsuleCollider.height * 0.5f), targetPoint, out hitinfo, groundLayer))
    202.                 {
    203.                     hitAngle = Vector3.Angle(Vector3.up, hitinfo.normal);
    204.  
    205.                     if (hitAngle > slopeLimit && hitAngle < 85f)
    206.                     {
    207.                         stopMove = true;
    208.                         return;
    209.                     }
    210.                 }
    211.             }
    212.             stopMove = false;
    213.         }
    214.  
    215.         public virtual void RotateToPosition(Vector3 position)
    216.         {
    217.             Vector3 desiredDirection = position - transform.position;
    218.             RotateToDirection(desiredDirection.normalized);
    219.         }
    220.  
    221.         public virtual void RotateToDirection(Vector3 direction)
    222.         {
    223.             RotateToDirection(direction, isStrafing ? strafeSpeed.rotationSpeed : freeSpeed.rotationSpeed);
    224.         }
    225.  
    226.         public virtual void RotateToDirection(Vector3 direction, float rotationSpeed)
    227.         {
    228.             if (!jumpAndRotate && !isGrounded) return;
    229.             direction.y = 0f;
    230.             Vector3 desiredForward = Vector3.RotateTowards(transform.forward, direction.normalized, rotationSpeed * Time.deltaTime, .1f);
    231.             Quaternion _newRotation = Quaternion.LookRotation(desiredForward);
    232.             transform.rotation = _newRotation;
    233.         }
    234.  
    235.         #endregion
    236.  
    237.         #region Jump Methods
    238.  
    239.         protected virtual void ControlJumpBehaviour()
    240.         {
    241.             if (!isJumping) return;
    242.  
    243.             jumpCounter -= Time.deltaTime;
    244.             if (jumpCounter <= 0)
    245.             {
    246.                 jumpCounter = 0;
    247.                 isJumping = false;
    248.             }
    249.             // apply extra force to the jump height  
    250.             var vel = _rigidbody.velocity;
    251.             vel.y = jumpHeight;
    252.             _rigidbody.velocity = vel;
    253.         }
    254.  
    255.         public virtual void AirControl()
    256.         {
    257.             if ((isGrounded && !isJumping)) return;
    258.             if (transform.position.y > heightReached) heightReached = transform.position.y;
    259.             inputSmooth = Vector3.Lerp(inputSmooth, input, airSmooth * Time.deltaTime);
    260.  
    261.             if (jumpWithRigidbodyForce && !isGrounded)
    262.             {
    263.                 _rigidbody.AddForce(moveDirection * airSpeed * Time.deltaTime, ForceMode.VelocityChange);
    264.                 return;
    265.             }
    266.  
    267.             moveDirection.y = 0;
    268.             moveDirection.x = Mathf.Clamp(moveDirection.x, -1f, 1f);
    269.             moveDirection.z = Mathf.Clamp(moveDirection.z, -1f, 1f);
    270.  
    271.             Vector3 targetPosition = _rigidbody.position + (moveDirection * airSpeed) * Time.deltaTime;
    272.             Vector3 targetVelocity = (targetPosition - transform.position) / Time.deltaTime;
    273.  
    274.             targetVelocity.y = _rigidbody.velocity.y;
    275.             _rigidbody.velocity = Vector3.Lerp(_rigidbody.velocity, targetVelocity, airSmooth * Time.deltaTime);
    276.         }
    277.  
    278.         protected virtual bool jumpFwdCondition
    279.         {
    280.             get
    281.             {
    282.                 Vector3 p1 = transform.position + _capsuleCollider.center + Vector3.up * -_capsuleCollider.height * 0.5F;
    283.                 Vector3 p2 = p1 + Vector3.up * _capsuleCollider.height;
    284.                 return Physics.CapsuleCastAll(p1, p2, _capsuleCollider.radius * 0.5f, transform.forward, 0.6f, groundLayer).Length == 0;
    285.             }
    286.         }
    287.  
    288.         #endregion
    289.  
    290.         #region Ground Check              
    291.  
    292.         protected virtual void CheckGround()
    293.         {
    294.             CheckGroundDistance();
    295.             ControlMaterialPhysics();
    296.  
    297.             if (groundDistance <= groundMinDistance)
    298.             {
    299.                 isGrounded = true;
    300.                 if (!isJumping && groundDistance > 0.05f)
    301.                     _rigidbody.AddForce(transform.up * (extraGravity * 2 * Time.deltaTime), ForceMode.VelocityChange);
    302.  
    303.                 heightReached = transform.position.y;
    304.             }
    305.             else
    306.             {
    307.                 if (groundDistance >= groundMaxDistance)
    308.                 {
    309.                     // set IsGrounded to false
    310.                     isGrounded = false;
    311.                     // check vertical velocity
    312.                     verticalVelocity = _rigidbody.velocity.y;
    313.                     // apply extra gravity when falling
    314.                     if (!isJumping)
    315.                     {
    316.                         _rigidbody.AddForce(transform.up * extraGravity * Time.deltaTime, ForceMode.VelocityChange);
    317.                     }
    318.                 }
    319.                 else if (!isJumping)
    320.                 {
    321.                     _rigidbody.AddForce(transform.up * (extraGravity * 2 * Time.deltaTime), ForceMode.VelocityChange);
    322.                 }
    323.             }
    324.         }
    325.  
    326.         protected virtual void ControlMaterialPhysics()
    327.         {
    328.             // change the physics material to very slip when not grounded
    329.             _capsuleCollider.material = (isGrounded && GroundAngle() <= slopeLimit + 1) ? frictionPhysics : slippyPhysics;
    330.  
    331.             if (isGrounded && input == Vector3.zero)
    332.                 _capsuleCollider.material = maxFrictionPhysics;
    333.             else if (isGrounded && input != Vector3.zero)
    334.                 _capsuleCollider.material = frictionPhysics;
    335.             else
    336.                 _capsuleCollider.material = slippyPhysics;
    337.         }
    338.  
    339.         protected virtual void CheckGroundDistance()
    340.         {
    341.             if (_capsuleCollider != null)
    342.             {
    343.                 // radius of the SphereCast
    344.                 float radius = _capsuleCollider.radius * 0.9f;
    345.                 var dist = 10f;
    346.                 // ray for RayCast
    347.                 Ray ray2 = new Ray(transform.position + new Vector3(0, colliderHeight / 2, 0), Vector3.down);
    348.                 // raycast for check the ground distance
    349.                 if (Physics.Raycast(ray2, out groundHit, (colliderHeight / 2) + dist, groundLayer) && !groundHit.collider.isTrigger)
    350.                     dist = transform.position.y - groundHit.point.y;
    351.                 // sphere cast around the base of the capsule to check the ground distance
    352.                 if (dist >= groundMinDistance)
    353.                 {
    354.                     Vector3 pos = transform.position + Vector3.up * (_capsuleCollider.radius);
    355.                     Ray ray = new Ray(pos, -Vector3.up);
    356.                     if (Physics.SphereCast(ray, radius, out groundHit, _capsuleCollider.radius + groundMaxDistance, groundLayer) && !groundHit.collider.isTrigger)
    357.                     {
    358.                         Physics.Linecast(groundHit.point + (Vector3.up * 0.1f), groundHit.point + Vector3.down * 0.15f, out groundHit, groundLayer);
    359.                         float newDist = transform.position.y - groundHit.point.y;
    360.                         if (dist > newDist) dist = newDist;
    361.                     }
    362.                 }
    363.                 groundDistance = (float)System.Math.Round(dist, 2);
    364.             }
    365.         }
    366.  
    367.         public virtual float GroundAngle()
    368.         {
    369.             var groundAngle = Vector3.Angle(groundHit.normal, Vector3.up);
    370.             return groundAngle;
    371.         }
    372.  
    373.         public virtual float GroundAngleFromDirection()
    374.         {
    375.             var dir = isStrafing && input.magnitude > 0 ? (transform.right * input.x + transform.forward * input.z).normalized : transform.forward;
    376.             var movementAngle = Vector3.Angle(dir, groundHit.normal) - 90;
    377.             return movementAngle;
    378.         }
    379.  
    380.         #endregion
    381.  
    382.         [System.Serializable]
    383.         public class vMovementSpeed
    384.         {
    385.             [Range(1f, 20f)]
    386.             public float movementSmooth = 6f;
    387.             [Range(0f, 1f)]
    388.             public float animationSmooth = 0.2f;
    389.             [Tooltip("Rotation speed of the character")]
    390.             public float rotationSpeed = 16f;
    391.             [Tooltip("Character will limit the movement to walk instead of running")]
    392.             public bool walkByDefault = false;
    393.             [Tooltip("Rotate with the Camera forward when standing idle")]
    394.             public bool rotateWithCamera = false;
    395.             [Tooltip("Speed to Walk using rigidbody or extra speed if you're using RootMotion")]
    396.             public float walkSpeed = 2f;
    397.             [Tooltip("Speed to Run using rigidbody or extra speed if you're using RootMotion")]
    398.             public float runningSpeed = 4f;
    399.             [Tooltip("Speed to Sprint using rigidbody or extra speed if you're using RootMotion")]
    400.             public float sprintSpeed = 6f;
    401.         }
    402.     }
    403. }
    Do you see it clearer?
     
  4. AdamGMain

    AdamGMain

    Joined:
    Jun 5, 2019
    Posts:
    8
    Looking at the motor script, it appears the speed variables aren't actually set in the Motor class, but in a separate class defined at the bottom as vMovementSpeed.

    On your roll and dash script can you try creating a serialized field for the the vMovementSpeed class?

    Would look like this:

    [Serialized Field] vMovementSpeed movementSpeed;

    Then drag the vThirdPersonMotor.cs script into that field in the inspector.

    You should then be able to reference that variable by calling movementSpeed.runningSpeed in your roll and dash script.
     
  5. D42mon

    D42mon

    Joined:
    Jul 16, 2021
    Posts:
    12
    Hello, thanks for your reply, however I have tried this as well and it does not work either.

    So far, the only partial solution I found was to declare this:
    Code (CSharp):
    1. public Invector.vCharacterController.vThirdPersonMotor.vMovementSpeed myMotor;
    And access it like this:
    Code (CSharp):
    1. myMotor.runningSpeed = 150f;
    Sadly, it just adds it to the RollAndDash component without actually setting or changing anything (but at least it doesn't break my code)...

    EDIT: After tweaking a few things, I decided to incorporate my dash directly into the vThirdPersonMotor.cs script. So I declared my variables from the original RollAndDash.cs script and then transformed this:
    Code (CSharp):
    1. public virtual void SetControllerMoveSpeed(vMovementSpeed speed)
    2.         {
    3.             if (speed.walkByDefault)
    4.                 moveSpeed = Mathf.Lerp(moveSpeed, isSprinting ? speed.runningSpeed : speed.walkSpeed, speed.movementSmooth * Time.deltaTime);
    5.             else
    6.                 moveSpeed = Mathf.Lerp(moveSpeed, isSprinting ? speed.sprintSpeed : speed.runningSpeed, speed.movementSmooth * Time.deltaTime);
    7.         }
    to this:
    Code (CSharp):
    1. public virtual void SetControllerMoveSpeed(vMovementSpeed speed)
    2.         {
    3.             if (speed.walkByDefault)
    4.                 moveSpeed = Mathf.Lerp(moveSpeed, isSprinting ? speed.runningSpeed : speed.walkSpeed, speed.movementSmooth * Time.deltaTime);
    5.             else
    6.                 moveSpeed = Mathf.Lerp(moveSpeed, isSprinting ? speed.sprintSpeed : speed.runningSpeed, speed.movementSmooth * Time.deltaTime);
    7.  
    8.             if (Input.GetKeyDown(KeyCode.LeftControl) && !Input.GetKeyDown(KeyCode.Space) && m_Animator.GetBool("IsGrounded") == true)
    9.             {
    10.  
    11.                 player.GetComponent<Animator>().SetFloat("IsRolling", 1);
    12.                 m_Animator.Play("IsRolling");
    13.                 speed.runningSpeed = 150f;
    14.  
    15.             }
    16.         }
    It is probably funky but at least it works...
     
    Last edited: Sep 16, 2021
  6. AdamGMain

    AdamGMain

    Joined:
    Jun 5, 2019
    Posts:
    8
    The only thing you have to really watch out for is that if you update your Invector character controller, it's going to break what you are trying to do.