Hi I’m new to unity 3d andnew to coding… I know a little of lua but now I’m trying to learn unity3d and c# and JS a little
so I’m trying to create my own personal raycast pickup and drag script to add to my character
after reading a bit of tutorial and watched some youtube videos i have this
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DragRigidbodyUp : MonoBehaviour {
public GameObject playerCam;
public string GrabButton = "Grab";
public string ThrowButton = "Throw";
public float PickupRange = 3f;
public float ThrowStrength = 50f;
public float distance = 3f;
public float maxDistanceGrab = 4f;
private Ray playerAim;
private GameObject objectHeld;
private bool isObjectHeld;
void Start () {
isObjectHeld = false;
objectHeld = null;
}
void FixedUpdate () {
if(Input.GetButton(GrabButton)){
if(!isObjectHeld){
tryPickObject();
} else {
holdObject();
}
}else if(isObjectHeld){
DropObject();
}
if(Input.GetButton(ThrowButton) && isObjectHeld){
isObjectHeld = false;
objectHeld.GetComponent<Rigidbody>().useGravity = true;
ThrowObject();
}
}
private void tryPickObject(){
Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if (Physics.Raycast (playerAim, out hit, PickupRange)){
if(hit.collider.tag == "Pickupable"){
isObjectHeld = true;
objectHeld = hit.collider.gameObject;
objectHeld.GetComponent<Rigidbody>().useGravity = false;
objectHeld.GetComponent<Rigidbody>().freezeRotation = true;
}
}
}
private void holdObject(){
Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
Vector3 nextPos = playerCam.transform.position + playerAim.direction * distance;
Vector3 currPos = objectHeld.transform.position;
objectHeld.GetComponent<Rigidbody>().velocity = (nextPos - currPos) * 10;
if (Vector3.Distance(objectHeld.transform.position, playerCam.transform.position) > maxDistanceGrab)
{
DropObject();
}
}
private void DropObject()
{
isObjectHeld = false;
objectHeld.GetComponent<Rigidbody>().useGravity = true;
objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
objectHeld = null;
}
private void ThrowObject()
{
objectHeld.GetComponent<Rigidbody>().AddForce(playerCam.transform.forward * ThrowStrength);
objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
objectHeld = null;
}
}
but when I click on the object nothing happen
can someone help me…
thanks in advance