Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Picking up Object

Discussion in 'Scripting' started by ShaunChad99, May 12, 2020.

  1. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    when i pick up my object and my player is close to another object it picks them up. can i pick up items using the raycast script i have


    raycast
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. namespace PlayerFeatures
    7. {
    8.     public class Player : MonoBehaviour
    9.     {
    10.         //Raycast
    11.         public float Maxdistance = 10;
    12.         public LayerMask layermask;
    13.         public bool ObjectHit = false;
    14.         GameObject HitObject;
    15.  
    16.         void Start()
    17.         {
    18.             ObjectHit = false;
    19.         }
    20.        
    21.         public void Update()
    22.         {
    23.             //Raycasting
    24.             RaycastHit hit;
    25.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    26.  
    27.             if (Physics.Raycast(ray, out hit, Maxdistance, layermask))
    28.             {
    29.                 if (hit.collider != null)
    30.                 {
    31.                     ObjectHit = true;
    32.                     print(hit.transform.name);
    33.                 }
    34.             }
    35.             ObjectHit = hit.collider != null;
    36.         }
    37.     }
    38. }
    Pick Up Script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UiData;
    5. using PlayerFeatures;
    6.  
    7. namespace PickedupObjectData
    8. {
    9.  
    10.     public class PickUpAndThrow : MonoBehaviour
    11.     {
    12.         public Transform player;
    13.         public GameObject theDest;
    14.         public GameObject popupText;
    15.         public float throwForce = 10;
    16.         bool hasPlayer = false;
    17.         public bool beingCarried = false;
    18.         private bool touchedWall = false;
    19.         public Rigidbody rb;
    20.         Player RaycastHit;
    21.  
    22.         //Rotate
    23.         public float rotateSpeed = 1;
    24.  
    25.         void Start()
    26.         {
    27.             rb = GetComponent<Rigidbody>();
    28.         }
    29.  
    30.         void Update()
    31.         {
    32.             float dist = Vector3.Distance(gameObject.transform.position, player.position);
    33.             if (dist <= 2.5f)
    34.             {
    35.                 hasPlayer = true;
    36.             }
    37.             else
    38.             {
    39.                 hasPlayer = false;
    40.             }
    41.             if (hasPlayer && Input.GetButtonDown("Use"))
    42.             {
    43.                 GetComponent<Rigidbody>().useGravity = false;
    44.                 GetComponent<Rigidbody>().isKinematic = true;
    45.                 this.transform.parent = GameObject.Find("Item Dest").transform;
    46.                 gameObject.tag = "ItemInHand";
    47.                 beingCarried = true;
    48.             }
    49.             if (beingCarried)
    50.             {
    51.                 if (touchedWall)
    52.                 {
    53.                     GetComponent<Rigidbody>().useGravity = true;
    54.                     GetComponent<Rigidbody>().isKinematic = false;
    55.                     this.transform.parent = null;
    56.                     beingCarried = false;
    57.                     touchedWall = false;
    58.                 }
    59.                 else if (Input.GetButtonDown("Drop"))
    60.                 {
    61.                     GetComponent<Rigidbody>().useGravity = true;
    62.                     GetComponent<Rigidbody>().isKinematic = false;
    63.                     this.transform.parent = null;
    64.                     beingCarried = false;
    65.                 }
    66.             }
    67.  
    68.             //Rotate
    69.             RotateObject();
    70.         }
    71.  
    72.         void RotateObject()
    73.         {
    74.             if(gameObject.tag == "ItemInHand")
    75.             {
    76.                 if (Input.GetMouseButton(1) && Input.GetAxis("Mouse ScrollWheel") > 0)
    77.                 {
    78.                     transform.Rotate(0.0f, 0.0f, -1.0f * rotateSpeed, Space.Self);
    79.                 }
    80.                 if (Input.GetMouseButton(1) && Input.GetAxis("Mouse ScrollWheel") < 0)
    81.                 {
    82.                     transform.Rotate(0.0f, 0.0f, 1.0f * rotateSpeed, Space.Self);
    83.                 }
    84.                 if (Input.GetMouseButton(0) && Input.GetAxis("Mouse ScrollWheel") > 0)
    85.                 {
    86.                     transform.Rotate(1.0f, 0.0f, 0.0f * rotateSpeed, Space.Self);
    87.                 }
    88.                 if (Input.GetMouseButton(0) && Input.GetAxis("Mouse ScrollWheel") < 0)
    89.                 {
    90.                     transform.Rotate(-1.0f, 0.0f, 0.0f * rotateSpeed, Space.Self);
    91.                 }
    92.             }
    93.            
    94.         }
    95.  
    96.         void OnTriggerEnter()
    97.         {
    98.             if (beingCarried)
    99.             {
    100.                 touchedWall = true;
    101.             }
    102.         }
    103.     }
    104. }
    105.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    What's the question here exactly?
     
  3. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    when i look at one object and press "E" to pick up it picks all objects up

    upload_2020-5-13_17-16-9.png
     
  4. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    exactly what you need i thinK: