So I have a script and at the end of all the code I put a translate thing so it goes up high in the air. For some reason it isn’t working. I even put a debug.log after it and it ran, so the code was executing. does anyone know why?
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using Vector3 = UnityEngine.Vector3;
public class PickUp1 : MonoBehaviour
{
public static bool IngredientCollected1 = false;
void Update()
{
RaycastHit hit;
if (Input.GetKeyDown("e"))
{
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
{
Debug.Log("Raycast Hit!");
if (hit.transform.CompareTag("Selectable")) Debug.Log("Object is selectable");
{
Debug.Log("If statement successful");
PickUpFunction(hit.transform);
}
}
}
}
public void PickUpFunction(Transform targ)
{
var rb = targ.GetComponent<Rigidbody>();
var rend = targ.GetComponent<MeshRenderer>();
var col = targ.GetComponent<BoxCollider>();
if (rb != null) rb.useGravity = false;
IngredientCollected1 = true;
targ.transform.Translate(0, 1000, 0);
Debug.Log("object moved");
}
}
If that doesn’t help then maybe try PickUpFunction(hit.rigidbody.gameObject.transform) or
Okay so it turns out I was wrong and that PickUpFunction(hit.transform) should work perfectly fine … in fact it might even be the most correct way of doing it (sry I’m wayyy too sleep deprived).
These lines look suspicious, I have never seen an if statement like that before :
if (hit.transform.CompareTag("Selectable")) Debug.Log("Object is selectable");
{
Maybe put the Debug.Log inside the body {} … but I doubt that’s the bug.
Does Debug.Log(“object moved”); get called?
Sorry I can’t be of more help, nothing seems to jump out at me as being wrong with the code. Perhaps you have another script interfering and resetting its position? That’s a common problem.
Log the name of the GameObject you are translating. Also you can pass the GameObject as a second parameter to Debug.Log. if you do you will be able to click the log message and unity will highlight that object in the scene hierarchy.
Now you figure out why and fix it. Seems like your raycast is hitting your player and you aren’t expecting that. Why? What’s your layermask set to and is the player’s layer part of it?
If you want to quickly ignore the player in the Raycast (aside from using layers and layermasks) you can just set collider.enabled false directly before the raycast, then set back to true right after.
For some reason when I copy and pasted the code, some of it was indented. I can try and paste it again
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using Vector3 = UnityEngine.Vector3;
public class PickUp1 : MonoBehaviour
{
public static bool IngredientCollected1 = false;
void Update()
{
RaycastHit hit;
if (Input.GetKeyDown("e"))
{
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
{
Debug.Log("Raycast Hit!");
if (hit.transform.CompareTag("Selectable")) Debug.Log("Object is selectable");
{
Debug.Log("If statement successful");
PickUpFunction(hit.collider.gameObject.transform);
}
}
}
}
public void PickUpFunction(Transform targ)
{
var rb = targ.GetComponent<Rigidbody>();
var rend = targ.GetComponent<MeshRenderer>();
var col = targ.GetComponent<BoxCollider>();
if (rb != null) rb.useGravity = false;
IngredientCollected1 = true;
targ.position += new Vector3(0, 1000, 0); Debug.Log(targ + "is the gameobject");
Debug.Log("object moved");
}
}