I’ve spent hours on this, but I just can’t figure out what I’m doing wrong.
I’d just like to display some text when the player collides with a sphere.
Here’s the code I have on the sphere:
public class CollisionText : MonoBehaviour
{
private Text myText;
void Start()
{
myText.enabled = false;
}
// Assuming you're using a 2D platform
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Player")
{
myText.enabled = true;
}
}
void OnCollisionExit2D(Collision2D collision)
{
if(collision.gameObject.tag == "Player")
{
myText.enabled = false;
}
}
}
Here’s my player code as well in case there’s something in there that messes with the sphere’s code:
public class PlayerController : MonoBehaviour
{
[SerializeField] Transform playerCamera;
[SerializeField] float mouseSensitivity = 3.5f;
float cameraPitch = 0.0f;
[SerializeField] bool lockCursor = true;
[SerializeField] float walkSpeed = 6.0f;
[SerializeField][Range(0.0f, 0.5f)] float smoothTime = 0.3f;
[SerializeField][Range(0.0f, 0.5f)] float MousesmoothTime = 0.03f;
[SerializeField] float gravity = -13.0f;
CharacterController controller = null;
Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
float velocityY = 0.0f;
void Start()
{
controller = GetComponent<CharacterController>();
if(lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
// Update is called once per frame
void Update()
{
MouseLook();
playerMovement();
}
void MouseLook()
{
Vector2 targetmouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetmouseDelta, ref currentMouseDeltaVelocity, MousesmoothTime);
cameraPitch -= targetmouseDelta.y * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
playerCamera.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * targetmouseDelta.x * mouseSensitivity);
}
void playerMovement()
{
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, smoothTime);
Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
if(controller.isGrounded)
{
velocityY = 0.0f;
}
velocityY += gravity + Time.deltaTime;
}
}
I’d appreciate any help. Thanks!