EDIT: I initially said that I made the change in one part of the script and it worked as expected and in another part of the script it did not. This is actually not true. I made the change on a different script which is also a component of GameObject Player
. But I am still expecting the same outcomes as I describe below.
I am new to programming and Unity. I’m looking for help understanding some behavior that I’m not expecting. I recently changed some of the code in my movement script around so that it is controlling the parent GameObject Player
instead of the child GameObject PlayerGraphics
.
The script is a component of the parent GameObject Player
.
In some parts of my code I was able to simply change playerGraphics
reference to transform
–the way I understand this is that transform
references the Transform that is attached to the GameObject that the script is attached to (in this case Player
).
In the code below, why does the position stop updating after changing from playerGraphics.position
to transform.position
, but in other parts of my code it works as I expect when making the same change? Thank you for your help!
// CODE A
// Keep Player Camera Controller transform in same world position as Player transform
void CameraControllerToPlayer() {
playerCameraController.position = playerGraphics.position;
}
// CODE B
// Keep Player Camera Controller transform in same world position as Player transform
void CameraControllerToPlayer() {
playerCameraController.position = transform.position;
}
// FULL SCRIPT
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerCursor : MonoBehaviour
// The Player Interact script contains functionality for player interaction
// with the game world.
{
public InputMaster controls;
public Transform playerGraphics;
public Transform playerCameraController;
[SerializeField] float lookSpeed = 100f;
[SerializeField] private string targetableTag = "Targetable";
[SerializeField] private Material highlightMaterial;
[SerializeField] private Material defaultMaterial;
bool cameraGrabbed = false;
bool playerGrabbed = false;
Vector2 cursorPosition;
float xRotation = 0f;
float yRotation = 0f;
private Transform _selection;
Transform mouseOverTarget = null;
void Start() {
}
void Awake() {
controls = new InputMaster();
// started called on button press
controls.Player.GrabCamera.started += _ => GrabCamera();
controls.Player.GrabPlayer.started += _ => GrabPlayer();
// performed called on button release
controls.Player.GrabCamera.performed += _ => ReleaseCamera();
controls.Player.GrabPlayer.performed += _ => ReleasePlayer();
}
void Update() {
MouseOver();
CameraControllerToPlayer();
Steer();
Look();
Debug.Log(mouseOverTarget);
}
Transform MouseOver() {
if (!Cursor.visible) {
_selection = null;
}
else {
var ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
var hitTarget = hit.transform;
if (hitTarget.CompareTag(targetableTag)) {
mouseOverTarget = hit.transform;
}
}
else {
Transform mouseOverTarget = null;
}
}
return mouseOverTarget;
}
// void MouseOver() {
// if (!Cursor.visible) {
// _selection = null;
// }
// else{
// var ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
// RaycastHit hit;
// if (_selection != null) {
// var selectionRenderer = _selection.GetComponent<Renderer>();
// selectionRenderer.material = defaultMaterial;
// _selection = null;
// }
// if (Physics.Raycast(ray, out hit)) {
// var mouseOverTarget = hit.transform;
// // Check for targetable tag
// if (mouseOverTarget.CompareTag(targetableTag)) {
// // Get renderer from target object
// var selectionRenderer = mouseOverTarget.GetComponent<Renderer>();
// // Check target renderer exists
// if (selectionRenderer != null) {
// // Change renderer to the highlight material
// selectionRenderer.material = highlightMaterial;
// // Create variable to hold target
// _selection = mouseOverTarget;
// }
// }
// }
// }
// }
void CursorSelect() {
}
void CursorInteract() {
}
void Steer() {
if(playerGrabbed) {
Vector3 graphicsRotationEuler = playerCameraController.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(0f, graphicsRotationEuler.y, 0f);
}
}
void Look() {
if(cameraGrabbed || playerGrabbed) {
Vector2 mouseLook = controls.Player.MouseLook.ReadValue<Vector2>();
float mouseX = mouseLook.x * lookSpeed * Time.deltaTime;
float mouseY = mouseLook.y * lookSpeed * Time.deltaTime;
xRotation -= mouseY;
yRotation += mouseX;
xRotation = Mathf.Clamp(xRotation, -89f, 89f);
playerCameraController.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
if(Mathf.Abs(mouseLook.x) > 0 || Mathf.Abs(mouseLook.y) > 0) {
Cursor.visible = false;
}
}
}
// Keep Player Camera Controller transform in same world position as Player transform
void CameraControllerToPlayer() {
playerCameraController.position = playerGraphics.position;
}
void GrabCamera() {
cameraGrabbed = true;
if (!playerGrabbed) {
// Save cursor location to cursorPosition
cursorPosition = controls.Player.MousePosition.ReadValue<Vector2>();
}
}
void GrabPlayer() {
playerGrabbed = true;
if (!cameraGrabbed) {
// Save cursor location to cursorPosition
cursorPosition = controls.Player.MousePosition.ReadValue<Vector2>();
}
}
void ReleaseCamera() {
cameraGrabbed = false;
Mouse.current.WarpCursorPosition(cursorPosition);
if (!playerGrabbed) {
Cursor.visible = true;
}
}
void ReleasePlayer() {
playerGrabbed = false;
Mouse.current.WarpCursorPosition(cursorPosition);
if (!cameraGrabbed) {
Cursor.visible = true;
}
}
void OnEnable() {
controls.Enable();
}
void onDisable() {
controls.Disable();
}
}