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

Having trouble accessing member in struct under namespace

Discussion in '2D' started by PhaazeReborn, May 16, 2020.

  1. PhaazeReborn

    PhaazeReborn

    Joined:
    Apr 11, 2020
    Posts:
    10
    Hi all!

    I am very new in using Unity. I am working on my first 2D game project and stumbled upon an issue. I'm still in the learning phase of using C# which I am not quite familiar with (C++ guy).

    I am basically try to access a member inside of a struct that is under a namespace. In addition, please let me know what I can do better with my player controller script as well :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Runtime.ConstrainedExecution;
    4. using TMPro.Examples;
    5. using UnityEngine;
    6. using UnityEngine.Experimental.XR;
    7. using PlayerBoundary;
    8.  
    9. namespace PlayerBoundary
    10. {
    11.     public class Boundary
    12.     {
    13.         float xMin;
    14.         float xMax;
    15.         float yMin;
    16.         float yMax;
    17.     }
    18. }
    19.  
    20. public class PlayerMovement : MonoBehaviour
    21. {
    22.  
    23.     [SerializeField] private float speed = 20.0f;
    24.     PlayerBoundary.Boundary boundary = new PlayerBoundary.Boundary();
    25.  
    26.  
    27.     Rigidbody2D pos;
    28.     void FixedUpdate()
    29.     {
    30.         float horizontalInput = Input.GetAxis("Horizontal");
    31.         float verticalInput = Input.GetAxis("Vertical");
    32.  
    33.         Vector2 movement = new Vector2(horizontalInput, verticalInput);
    34.         transform.Translate(movement * Time.deltaTime * speed);
    35.  
    36.         // Add boundary to limit player's movement
    37.         pos.position = new Vector2
    38.         {
    39.             Mathf.Clamp(pos.position, PlayerBoundary.Boundary.);  // <-- Here
    40.         };
    41.        
    42.        
    43.     }
    44. }
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Instead of PlayerBoundry.Boundry you need to use the field you setup called boundry. You are trying to access the struct through its definition, which is for getting static members or calling static methods.

    Edit: FixedUpdate is more for physics and seeing as your not using physics, change that too Update instead.
     
  3. PhaazeReborn

    PhaazeReborn

    Joined:
    Apr 11, 2020
    Posts:
    10

    Okay I see. How is this? Although i don't think it is possible to rotate the ship while movingg lefft-right?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using PlayerBounding;
    4. using UnityEngine;
    5. namespace PlayerBounding
    6. {
    7.     [System.Serializable]
    8.     public class Boundary
    9.     {
    10.         public float xMin;
    11.         public float xMax;
    12.         public float yMin;
    13.         public float yMax;
    14.     }
    15. }
    16. public class PlayerMovement : MonoBehaviour
    17. {
    18.  
    19.     public Boundary boundary;
    20.     public float speed;
    21.     public float tilt;
    22.     public Rigidbody2D rid;
    23.     private void Start()
    24.     {
    25.         rid = GetComponent<Rigidbody2D>();
    26.      
    27.     }
    28.     void FixedUpdate()
    29.     {
    30.         float horizontalInput = Input.GetAxis("Horizontal");
    31.         float verticalInput = Input.GetAxis("Vertical");
    32.         Vector2 movement = new Vector2(horizontalInput, verticalInput);
    33.         transform.Translate(movement * Time.fixedDeltaTime * speed);
    34.         // Add boundary to limit player's movement
    35.        transform.position = new Vector3(Mathf.Clamp(transform.position.x, boundary.xMin, boundary.xMax),
    36.                                         Mathf.Clamp(transform.position.y, boundary.yMin, boundary.yMax),transform.position.z);
    37.        // Not working as expected.
    38.        // GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    39.     }
    40. }
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    If you are using physics you need to move it with RigidBody and not through the transform directly, which causes collision detection to not function correctly or even not at all.

    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3.      float horizontalInput = Input.GetAxis("Horizontal");
    4.      float verticalInput = Input.GetAxis("Vertical");
    5.      Vector2 movement =new Vector2(horizontalInput, verticalInput);
    6.  
    7.     rid.velocity = movement * speed;//No need for time multiplication it will be multiplied with delta time automagically.
    8.     //or
    9.     rid.MovePosition(rid.position + movement * Time.fixedDeltaTime * speed);
    10.     //or
    11.     rid.AddForce(movement * speed);//This will cause the object to have acceleration and continue to accelerate unless you restrict its velocity. You should change out the speed value for an acceleration value here.
    12.     if (rid.velocity.sqrMagnitude > speed * speed)//This will restrict it so it doesn't keep accelerating. Getting the square magnitude is faster than magnitude as it doesn't need to do the square root, so we square the speed instead for a faster check.
    13.     {
    14.         rid.velocity = rid.velocity.normalized * speed;
    15.     }
    16.  
    17.     //This seems kinda hacky to me, would be better to setup edge colliders at the bounds and use those as physical bounds instead of this.
    18.     Vector3 boundsCorrectedPosition = new Vector3(
    19.         Mathf.Clamp(transform.position.x, boundary.xMin, boundary.xMax),
    20.         Mathf.Clamp(transform.position.y, boundary.yMin, boundary.yMax),
    21.         transform.position.z);
    22.     transform.position = boundsCorrectedPosition;
    23. }
     
  5. PhaazeReborn

    PhaazeReborn

    Joined:
    Apr 11, 2020
    Posts:
    10
    Thanks for this! I now know that the transform doesn't work with the physics engine, so I should've used rid.velocity instead.

    Many Thanks! :)