I made a code to pick up some items and there are some mistakes .
-
Whatever item I try to pick up, it only responds to 1 item (if I try to pick up item 1 or 2, it always moves item 1).
-
Whenever I try to pick up the only item that moves from the first problem the gravity turns off like it should, but if I try to pick up another item the gravity doesn’t turn off.
Any help is much appreciated.
using UnityEngine;
using System.Collections;
public class PickUp : MonoBehaviour {
// The variables for the positions.
Offset to the character.
float OffsetPositionX = 1;
float OffsetPositionY = 1;
float OffsetPositionZ = 1;
// Relative position to the character.
float CharacterPositionX;
float CharacterPositionY;
float CharacterPositionZ;
// The Player and the tagged objects.
public GameObject CarryableObject;
public GameObject Character;
void Start() {
// The origin of the objects.
CarryableObject = GameObject.FindWithTag("Carryable");
Character = GameObject.Find("Character");
// The code for determining the position (Put in note mode to deactivate but keep it).
CharacterPositionX = Character.transform.position.x + OffsetPositionX;
CharacterPositionY = Character.transform.position.y + OffsetPositionY;
CharacterPositionZ = Character.transform.position.z + OffsetPositionZ;
}
void OnTriggerStay(Collider other) {
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;
}
if(Input.GetKeyDown("e")) {
// The gravity.
other.attachedRigidbody.useGravity = false;
}
}
}
The position the only moving item goes to:
Also, I think this might have something to do with the entire game and not just the code so here is a link to my game in the google drive.
link to the game files