Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

SphereCastAll hit.point are allways zero 1 Answer

Player falling through moving platform 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges