How to add a different crosshair when mouse over an item
Hi there. I found this script on a Unity 5 YouTube tutorial but i would also like to add a mouse over the object and grab crosshair and than default to my standard crosshair.
Script name: PickupScript.cs
using UnityEngine;
using System.Collections;
public class PickupScript : MonoBehaviour {
GameObject mainCamera;
bool carrying;
GameObject carriedObject;
public float distance;
public float smooth;
void Start () {
mainCamera = GameObject.FindWithTag("MainCamera");
}
void Update () {
if(carrying) {
carry(carriedObject);
checkDrop();
} else {
pickup();
}
}
void rotateObject() {
carriedObject.transform.Rotate(5,10,15);
}
void carry(GameObject o) {
o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
o.transform.rotation = Quaternion.identity;
}
void pickup() {
if(Input.GetKeyDown (KeyCode.E)) {
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.camera.ScreenPointToRay(new Vector3(x,y));
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) {
Pickupable p = hit.collider.GetComponent<Pickupable>();
if(p != null) {
carrying = true;
carriedObject = p.gameObject;
p.gameObject.rigidbody.useGravity = false;
}
}
}
}
void checkDrop() {
if(Input.GetKeyDown (KeyCode.E)) {
dropObject();
}
}
void dropObject() {
carrying = false;
carriedObject.gameObject.rigidbody.useGravity = true;
carriedObject = null;
}
}
Comment
Your answer
Follow this Question
Related Questions
Tilt/Rotation based on direction of movement? (player follows mouse hover) 0 Answers
In 3d multiplayer fps how can I make the crosshair such that other users can not see my crosshair? 1 Answer
Issues with OnMouseOver() not working 2 Answers
Shooting exactly to the center of screen 0 Answers
Remove button OnMouseOver highlighting 0 Answers