There are two scripts, “Docs” and “Letter”.
When you pick up a note, everything is ok, but when it needs to be returned to its place, it falls into the same coordinate, no matter how many notes on the stage they all fall into the same position.
I made a second script where I get the position of the object on which the script is.
Now the question arose of how to force the object to move to the position that was obtained through the “Letter” script
using UnityEngine;
using System.Collections;
public class Letter : MonoBehaviour {
public Vector3 pos;
public GameObject[] Letter_obj;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Letter_obj = GameObject.FindGameObjectsWithTag ("letter");
pos = Letter_obj[0].transform.position;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class Docs : MonoBehaviour
{
public LayerMask mask;
public FirstPersonController player;
private bool isread = false;
private Transform letter;
Vector3 StartPosition;
Quaternion StartRotation;
private Letter Letter_obj;
void Start () {
player.enabled = true;
StartPosition = Letter_obj.transform.GetComponent<Transform>().position;
StartRotation = Letter_obj.transform.GetComponent<Transform>().rotation;
}
void Update () {
if(!isread)
{
RaycastHit hit;
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 2f, mask))
{
if(hit.collider.tag == "letter")
Letter_obj = hit.collider.gameObject.GetComponent<Letter>();
{
if(Input.GetKeyDown(KeyCode.E))
{
player.enabled = false;
letter = hit.transform;
isread = true;
}
}
}
}
else
{
letter.position = Camera.main.transform.position + Camera.main.transform.forward * .5f;
letter.LookAt(Camera.main.transform);
if(Input.GetKeyDown(KeyCode.E))
{
player.enabled = true;
isread = false;
letter.transform.position = StartPosition;
letter.transform.rotation = StartRotation;
}
}
}
}