Hello,
this is the script I am using:
var mouseOverColor = Color.blue;
private var originalColor : Color;
function Start () {
originalColor = renderer.sharedMaterial.color;
}
function OnMouseEnter () {
renderer.material.color = mouseOverColor;
}
function OnMouseExit () {
renderer.material.color = originalColor;
}
function OnMouseDown () {
var screenSpace = Camera.main.WorldToScreenPoint(transform.position);
var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
while (Input.GetMouseButton(0))
{
var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
transform.position = curPosition;
yield;
}
}
The script currently lets me grab an object if I look at it and hold the mouse button.
I am trying to edit it so that I can hold and object by looking at it and pressing E.
If the player presses E again, the he will drop it (like in source games).
I have tried everything I can imagine, but nothing worked. Can someone help me?
Thanks.
var SpawnTo : Transform; //your hand for example, attack an object to your character that you want the position of what you picked up to go to
var Object1 : Transform; //what your picking up, the object that you want to move
var dist = 5; //distance at which you can pick things up
private var isHolding = false;
function Update () {
if(Input.GetKeyDown(KeyCode.E)){ //if you press 'e'
if(Vector3.Distance(transform.position, Object1.position) < dist) //if distance is less than dist variable
{
isHolding = !isHolding; //changes isHolding var from false to true
}
}
if(isHolding == true){
Object1.rigidbody.useGravity = false; //sets gravity to not on so it doesn't just fall to the ground
Object1.parent = SpawnTo; //parents the object
Object1.transform.position = SpawnTo.transform.position; //sets position
Object1.transform.rotation = SpawnTo.transform.rotation; //sets rotation
}
else{ //if isHolding isn't true
SpawnTo.transform.DetachChildren(); //detach child (object) from hand
Object1.rigidbody.useGravity = true; //add the gravity back on
}
}
private var isHighlighted : boolean = false;
var SpawnTo : Transform; //your hand for example, attack an object to your character that you want the position of what you picked up to go to
var Object1 : Transform; //what your picking up, the object that you want to move
var Object2: Transform;//second object you wantto move
var Object3: Transform;//third object you want to move
private var Alreadyholding= false;
var dist = 5; //distance at which you can pick things up
private var isHolding = false;
private var gameCharacter : Transform;
private var distance : float;
function Start() {
gameCharacter = GameObject.Find("3rd Person Controller").GetComponent(Transform);
}
function OnMouseEnter() {
if(distance <= 4.0) {
CreateInfoName();
renderer.material.color = Color.red;
isHighlighted = true;
}
}
function OnMouseExit() {
renderer.material.color = Color.white;
Destroy(GameObject.Find("infoName"));
isHighlighted = false;
}
function Update () {
distance = Mathf.Sqrt((gameCharacter.position - transform.position).sqrMagnitude);
if(Input.GetKey("e") && isHighlighted == true){
if(Vector3.Distance(transform.position, Object1.position) < dist) //if distance is less than dist variable
{
isHolding = !isHolding; //changes isHolding var from false to true
}
if(isHolding == true){
Object1.rigidbody.useGravity = false; //sets gravity to not on so it doesn't just fall to the ground
Object1.parent = SpawnTo; //parents the object
Object1.transform.position = SpawnTo.transform.position; //sets position
Object1.transform.rotation = SpawnTo.transform.rotation; //sets rotation
}
else{ //if isHolding isn't true
SpawnTo.transform.DetachChildren(); //detach child (object) from hand
Object1.rigidbody.useGravity = true; //add the gravity back on
}
}
}
function CreateInfoName(){
var infoName = new GameObject("infoName");
infoName.AddComponent(GUIText);
infoName.GetComponent(GUIText).text = gameObject.name;
infoName.transform.position = Vector3(0.5, 0.5, 0);
infoName.GetComponent(GUIText).alignment = TextAlignment.Center;
infoName.GetComponent(GUIText).anchor = TextAnchor.LowerCenter;
infoName.GetComponent(GUIText).pixelOffset = Vector2 (0, 25);
}