henke
henke
CC#
Created by henke on 1/28/2023 in #help
✅ C# unity rotation clamping.
So I want it to be able to rotate in a 180 degree angle from the left side and 180degree from the right side.. depending on where my mouse is at and which direction i'm facing. Basically limiting the angles the player can have depending on which side hes facing. The right side (changeDirection.x == 1) works. The left side (changeDireciton.x == -1) doesn't work. Any help or code rewrite suggestion appreciated !
// private void Update()
{

Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0f;

Vector3 aimDirection = (mousePos - transform.position).normalized;


if(changeDirection.x == -1)
{
angle = (Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 180f);
angle = Mathf.Clamp(angle, -90f, 90f);
print(angle);
}
else if(changeDirection.x == 1)
{
angle = (Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg);
angle = Mathf.Clamp(angle, -90f, 90f);
//print(Mathf.Rad2Deg);
}

zAngle = new Vector3(0, 0, angle);
transform.eulerAngles = zAngle;
}
// private void Update()
{

Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0f;

Vector3 aimDirection = (mousePos - transform.position).normalized;


if(changeDirection.x == -1)
{
angle = (Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 180f);
angle = Mathf.Clamp(angle, -90f, 90f);
print(angle);
}
else if(changeDirection.x == 1)
{
angle = (Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg);
angle = Mathf.Clamp(angle, -90f, 90f);
//print(Mathf.Rad2Deg);
}

zAngle = new Vector3(0, 0, angle);
transform.eulerAngles = zAngle;
}
5 replies
CC#
Created by henke on 1/23/2023 in #help
✅ Need help with C# unity game development.
The issue I have is that im getting reference null on this line: itemParentObjectTransform.transform.parent = playerController.transform;
//
//In my PlayerController : MonoBehaviour
if(Input.GetKeyDown(KeyCode.B))
{

GameObject[] itemObjects = GameObject.FindGameObjectsWithTag("Items");
foreach(GameObject itemObject in itemObjects)
{
// Retrieve the script component attached to the child object
ItemObject script = itemObject.GetComponentInParent<ItemObject>();
if(script != null)
{
// Call the function on the script
script.PickupItem();
}
}


//In my child of another GameObject::
public class ItemObject : MonoBehaviour
{
public PlayerController playerController;
public GameObject itemObject;
public Transform itemParentObjectTransform;
// bool Pickable = false;
bool isColliding = false;

private void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject.CompareTag("Player"))
{
isColliding = true;
}
}

private void OnTriggerExit2D(Collider2D collider)
{
if(collider.gameObject.CompareTag("Player"))
{
isColliding = false;
}
}

public void PickupItem()
{
print("function gets called");
if(isColliding == true)
{
print("works");
// Attach the item to the player
itemParentObjectTransform.transform.parent = playerController.transform;
// Change the position of the item to match the player's position
itemParentObjectTransform.transform.localPosition = Vector3.zero;
}
else
{
print("Theres no item to pickup nearby");
}
}
//
//In my PlayerController : MonoBehaviour
if(Input.GetKeyDown(KeyCode.B))
{

GameObject[] itemObjects = GameObject.FindGameObjectsWithTag("Items");
foreach(GameObject itemObject in itemObjects)
{
// Retrieve the script component attached to the child object
ItemObject script = itemObject.GetComponentInParent<ItemObject>();
if(script != null)
{
// Call the function on the script
script.PickupItem();
}
}


//In my child of another GameObject::
public class ItemObject : MonoBehaviour
{
public PlayerController playerController;
public GameObject itemObject;
public Transform itemParentObjectTransform;
// bool Pickable = false;
bool isColliding = false;

private void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject.CompareTag("Player"))
{
isColliding = true;
}
}

private void OnTriggerExit2D(Collider2D collider)
{
if(collider.gameObject.CompareTag("Player"))
{
isColliding = false;
}
}

public void PickupItem()
{
print("function gets called");
if(isColliding == true)
{
print("works");
// Attach the item to the player
itemParentObjectTransform.transform.parent = playerController.transform;
// Change the position of the item to match the player's position
itemParentObjectTransform.transform.localPosition = Vector3.zero;
}
else
{
print("Theres no item to pickup nearby");
}
}
Any help appreciated!
7 replies
CC#
Created by henke on 1/17/2023 in #help
✅ beginner help with unity
Deal: I am trying to take my health float value from a script that is "not" in the scene (just a script) and pass it the child of a Player Gameobject. I have tried to use GetComponent and some basic stuff but i dont know why its not working. I can use GetComponent from my Player object and get the health float but not from the chiöd script. Any help or advice appreciated!
26 replies