I am making a project where the character is trying to pick up a sword from a chest. I am using a raycast to gauge the distance from the character to the sword- when the character hits E, or “Input”, the sword in the chest should disappear and reappear in their hand. But this is not the case for my script. My sword will disappear in the chest, the reappear moments later- but the sword does not appear in their hand. Any help is greatly appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grabweapon : MonoBehaviour {
public GameObject FakeSword;
public GameObject RealSword;
private float TheDistance;
// Use this for initialization
void Start () {
RealSword.SetActive(false);
}
// Update is called once per frame
void Update () {
TheDistance = PlayerRaycast.DistanceFromTarget;
if (TheDistance <= 3) {
if(Input.GetButtonDown(“Input”));
RealSword.SetActive(true);
I would put in some debugs to make sure you are even triggering parts of your code. Assuming realsword is attached to the hand and moves along with the player, make sure that part of your code is being called. If not, make sure distance is correctly returning a value you expect. Unfortunately, what you’ve shown isn’t really enough to tell me much.
Thank you for a quick response! I put a few debugs in, and it isn’t being triggered. I do have a recast set up that works, but the sword isn’t being grabbed at all. I’m definitely new at this, and am having trouble scripting this particular code. Do you have any ideas/suggestions of how to go about this? If not that’s completely fine- I appreciate your first answer a ton.
Hello again! Sorry to bother you, but I’ve made some advancements in my code. I figured out why my distance and to target weren’t adjusting- now it’s just a matter of changing the code so the item on the floor is equipped when I hit E. Just an update