Question by
RVDL_IT · Apr 25, 2017 at 04:43 PM ·
raycastobjectbugif-statementsspherecast
Stubborn Pick Up Script
I got everything under the hood to work like the raycast but the actual pick up action doesn't happen. I tried to take the RayCast and the SphereCast if statements away but it didn't help. Help is appreciated.
My pick up script:
using UnityEngine;
using System.Collections;
using CameraRayCastLine;
namespace CarryingWithoutDrop {
public class PickUp : MonoBehaviour {
// The variables for the positions.
// Offset to the character.
public float OffsetPositionX = 1;
public float OffsetPositionY = 1;
public float OffsetPositionZ = 1;
// Relative position to the character.
float CharacterPositionX;
float CharacterPositionY;
float CharacterPositionZ;
// The Spherecast information.
// Relative position to the object for the SphereCast.
float ObjectPositionX;
float ObjectPositionY;
float ObjectPositionZ;
// The radius of the object SphereCast.
public float SphereCastRadius = 2.5f;
// The boolean that checks if the camera's RayCast is hit.
public bool CameraRayCast;
// The Player and the tagged objects.
private GameObject CarryableObject;
public GameObject Character;
public void Start() {
// The code that tells you how to pick up an object through the debug log.
Debug.Log("Press E to pick up an object.");
// The origin of the objects.
CarryableObject = GameObject.FindGameObjectWithTag("Carryable");
Character = GameObject.Find("Character");
// The code for determining the position of the character.
CharacterPositionX = Character.transform.position.x + OffsetPositionX;
CharacterPositionY = Character.transform.position.y + OffsetPositionY;
CharacterPositionZ = Character.transform.position.z + OffsetPositionZ;
// The code for determining the position of the object for the SphereCast.
ObjectPositionX = CarryableObject.transform.position.x;
ObjectPositionY = CarryableObject.transform.position.y;
ObjectPositionZ = CarryableObject.transform.position.z;
// The code that references the raycast boolean of the camera.
CameraRayCast = GameObject.Find("MainCamera").GetComponent<PickUpRayCast>().CameraRayCast;
}
void PickUpCode() {
// A RayCast variable.
RaycastHit hit;
// If you're in the SphereCast.
if(Physics.SphereCast(new Vector3 (ObjectPositionX, ObjectPositionY, ObjectPositionZ), SphereCastRadius, transform.forward, out hit, 10)) {
// If you're looking at the box collider trigger.
if (CameraRayCast == true) {
Debug.Log("Press E to pick up the" + CarryableObject.name + ".");
// If you press the E key.
if (Input.GetKeyDown("e")) {
// The position relative to the character.
CarryableObject.transform.position = new Vector3(CharacterPositionX, CharacterPositionY, CharacterPositionZ);
// The rotation relative to the character.
CarryableObject.transform.rotation = Character.transform.rotation;
// The gravity.
CarryableObject.GetComponent<Rigidbody>().useGravity = false;
// A message showing that the object was picked up.
Debug.Log("You picked up the " + CarryableObject.name + "! Good for you!");
Debug.Log("Press the left mouse button to use an object.");
Debug.Log("Press E to drop an object.");
}
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
2D Raycasting and interacting with 2D Objects, 0 Answers
Physic SphereCast hits null 1 Answer
Trigger animation when looking at an object? 1 Answer