Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Cant reference correct player speed

Discussion in 'Scripting' started by cuzzydino, May 25, 2019.

  1. cuzzydino

    cuzzydino

    Joined:
    Dec 4, 2017
    Posts:
    3
    Hey guys,

    I'm having trouble referencing my players speed in a 3d game i've been working on. I can reference the speed fine but the problem im having is the speed isnt increasing or decreasing, its just fixed at 250 (which is what the variable is actually set to) - im obviously either referencing the wrong variable but i'm lost on what to do and would really appreciate any help.

    My player movement script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     private Rigidbody rb;
    8.  
    9.     public float ballSpeed;
    10.     public float jumpForce;
    11.  
    12.     public LayerMask groundLayers;
    13.  
    14.     public SphereCollider col;
    15.  
    16.     public AudioClip jumpSound;
    17.     public AudioClip attackSound;
    18.  
    19.     void Start()
    20.     {
    21.         rb = GetComponent<Rigidbody>();
    22.         col = GetComponent<SphereCollider>();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void FixedUpdate ()
    27.     {
    28.         float xSpeed = Input.GetAxis("Horizontal");
    29.         float ySpeed = Input.GetAxis("Vertical"); //Controls
    30.  
    31.         Rigidbody body = GetComponent<Rigidbody>();
    32.  
    33.         body.AddTorque(new Vector3(xSpeed, 0, ySpeed) * ballSpeed * Time.deltaTime); //Force for control movement
    34.  
    35.         if (IsGrounded() && Input.GetKeyDown(KeyCode.Space)) //Jump
    36.         {
    37.             AudioSource.PlayClipAtPoint(jumpSound, transform.position);
    38.             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    39.         }
    40.         else
    41.         {
    42.             if (IsGrounded() == false && Input.GetKeyDown(KeyCode.Space)) //attack
    43.             {
    44.                 rb.AddForce(Vector3.up * 4, ForceMode.Impulse);
    45.                 AudioSource.PlayClipAtPoint(attackSound, transform.position);
    46.                 xSpeed = 0;
    47.                 ySpeed = 0;
    48.                 rb.AddForce(Vector3.down * 20, ForceMode.Impulse);
    49.             }
    50.  
    51.         }
    52.     }
    53.  
    54.     private bool IsGrounded() //bool for checking Player is grounded and not already mid air connected to an object
    55.     {
    56.         return Physics.CheckCapsule(col.bounds.center, new Vector3(col.bounds.center.x, col.bounds.min.y, col.bounds.center.z), col.radius * .9f, groundLayers);    
    57.     }
    58. }
    59.  

    And then my enemy script which i am trying to reference the speed in:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyScript : MonoBehaviour
    6. {
    7.     public AudioClip hurt;
    8.  
    9.     public int health = 5;
    10.     public int attackDamage = 2;
    11.     public float speed = 0;
    12.  
    13.     // Use this for initialization
    14.     void Start()
    15.     {
    16.         print(health);
    17.     }
    18.  
    19.     void FixedUpdate()
    20.     {
    21.         speed = GameObject.Find("Player").GetComponent<PlayerMovement>().ballSpeed;
    22.         print(speed);
    23.     }
    24.  
    25.     void OnCollisionEnter(Collision col)
    26.     {
    27.         if (col.gameObject.tag == "Player" && speed >= 260)
    28.         {
    29.             health -= attackDamage;
    30.             AudioSource.PlayClipAtPoint(hurt, transform.position);
    31.             print("touched" + health);
    32.         }
    33.  
    34.         if (health <= 0)
    35.         {
    36.             gameObject.SetActive(false);
    37.         }
    38.     }
    39. }
    I understand this is probably something simple but any help is appreciated
     
  2. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    513
    You're not changing the ballSpeed anywhere in your PlayerMovement script, so it's always going to have the same value when you reference it.
     
  3. cuzzydino

    cuzzydino

    Joined:
    Dec 4, 2017
    Posts:
    3
    Thanks for your response. I thought that would be the case but i dont know what else i can reference that would be the "real" speed. I've looked at the values and am pretty stumped, i thought maybe torque or some sort of velocity value would be there to reference but its not. Any ideas?
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,192
    The player's rigidbody component has a velocity property.
     
  5. cuzzydino

    cuzzydino

    Joined:
    Dec 4, 2017
    Posts:
    3
    Code (CSharp):
    1.     void OnCollisionEnter(Collision col)
    2.     {
    3.         Rigidbody rb = GetComponent<Rigidbody>(); //fetch physics properties from player
    4.         Vector3 vel = rb.velocity;
    5.         float speed = rb.velocity.magnitude; //this is the speed player is going
    6.  
    7.         print(speed);
    8.  
    9.         if (col.gameObject.tag == "Player" && speed >= 0.9)   //if speed/velocity is over 0.9 will hit
    10.         {
    11.             health -= attackDamage;
    12.             AudioSource.PlayClipAtPoint(hurt, transform.position);
    13.             print("touched" + health);
    14.         }
    15.  
    16.         if (health <= 0)
    17.         {
    18.             gameObject.SetActive(false);
    19.         }
    20.     }
    Here is the solution. Thanks for your replies guys