Hi guys,
I am working in Unity (C#) and I have a problem. I have a cube called littleOne that is falling down from start position and another cube that destroys the first cube OnTriggerEnter. When the first cube collides with "destroyer" cube, I want to bring it back to the start position. All game objects are attached correctly to the script. This is my solution:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
public GameObject littleOne;
public Vector3 startPoint;
void Start()
{
startPoint = littleOne.transform.position;
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.name == "littleOne") {
other.gameObject.transform.position = startPoint; //*not working
//littleOne.transform.position = startPoint; //*not Working
}
}
}
Lines that has symbol * does nothing, well with these lines I have a problem. I was searching for solution and I tried this way, but nothing happens.
EDIT: PROBLEM SOLVED: I have solved the problem. This code I have moved to a script with falling operation. It was issue in multiple related script to a object. Thank you for recommendations!
Other object and littleOne are referring to the same object which you are destroying:
Dont destroy it:
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.name == "littleOne")
{
littleOne.transform.position = startPoint;
}
}
Related
I am trying to create an interaction system in Unity using Natty Creations tutorial: https://www.youtube.com/watch?v=gPPGnpV1Y1c&t=866s
Although I am having a bit of trouble as the script cant find the Action that I am trying to reference.
I am still a beginner so sorry if this is a stupid question with a simple fix.
Here is the script that I am stuck with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInteract : MonoBehaviour
{
private Camera cam;
[SerializeField]
private float distance = 3f;
[SerializeField]
private LayerMask mask;
private PlayerUI playerUI;
private InputManager inputManager;
// Start is called before the first frame update
void Start()
{
cam = GetComponent<PlayerLook>().cam;
playerUI = GetComponent<PlayerUI>();
inputManager = GetComponent<InputManager>();
}
// Update is called once per frame
void Update()
{
playerUI.UpdateText(string.Empty);
Ray ray = new Ray(cam.transform.position, cam.transform.forward);
Debug.DrawRay(ray.origin, ray.direction * distance);
RaycastHit hitInfo; // var to store collision info
if (Physics.Raycast(ray, out hitInfo, distance, mask))
{
if (hitInfo.collider.GetComponent<Interactible>() != null)
{
Interactible interactible = hitInfo.collider.GetComponent<Interactible>();
playerUI.UpdateText(interactible.promptMessage);
if (inputManager.OnFoot.Interact.triggered)
{
interactible.BaseInteract();
}
}
}
}
And my Input Actions: Unity Input Actions
Any help would be greatly appreciated!
Thanks!
I had the SAMMMMEEE problem but I know the fix know (Thanks to a friend) all you have to do is to go into your InputManager and turn onFoot to public. In your Player Interact in the very last if function chane the Uppercase Onfoot to Lowercase onFoot. That's what works for me!
When you are in Input Actions you have to press Save Asset at the top of the window.
I don't know the problem but it worked for me when I did change "OnFoot" to "onFoot"(as Naeto3452 said) and if change "private" to "public" in InputManager.cs "private PlayerInput.OnFootActions onFoot;" it might help
so this issue is fixed, the problem was that the U in Update wasnt capitalised
me and a few friends started making a FPS a few weeks ago and I'm now trying to get the AI to just walk towards the player but the enemy just stands still like a random object.
I have tried to rewrite the code, redo the NavMesh, redo the player and enemy components (with help from my brother who is an indie dev), I followed unity's documentation, even tried to do the AI in a different project but it just doesn't work.
I don't get any errors, so I don't know what I'm doing wrong, been at this AI for a day or two now
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerNavMesh : MonoBehaviour{
private NavMeshAgent agent;
private GameObject target;
private void Start()
{
agent = GetComponent<NavMeshAgent>();
}
private void update() {
target = GameObject.FindGameObjectWithTag("Player");
if(target != null)
{
agent.destination = target.transform.position;
}
else
{
Debug.Log("No target found");
}
}
}
Update() should be capitalized. Also, why are you setting target every frame?
I'm making a game in Unity VR in which I punch numbers and get points.
The gloves are responsible for detecting collisions between the numbers that they hit. On my gloves I have a PunchScript component and the numbers each have a rigidBody and collider.
The problem is that no collisions seem to ever occur. I placed a Debug.LogError inside of the collision detection code to assert this.
I tried switching on/off kinematics on all objects and used different collision systems to no avail.
Here's my PunchScript component:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PunchScript : MonoBehaviour
{
public SteamVR_TrackedObject hand;
private Rigidbody rBody;
private bool visible = true;
// Start is called before the first frame update
void Start()
{
rBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
rBody.MovePosition(hand.transform.position);
rBody.MoveRotation(hand.transform.rotation);
// print(rBody.velocity.magnitude* 1000);
}
void OnCollisonEnter(Collision other)
{
Rigidbody otherR = other.gameObject.GetComponentInChildren<Rigidbody>();
if (other.gameObject.name == "frpnchbg") {
Debug.LogError("Hit!");
}
if (other == null)
return;
Vector3 avgPoint = Vector3.zero;
foreach (ContactPoint p in other.contacts) {
avgPoint += p.point;
}
avgPoint /= other.contacts.Length;
Vector3 dir = (avgPoint - transform.position).normalized;
otherR.AddForceAtPosition(dir *50f* rBody.velocity.magnitude, avgPoint);
}
}
Here's how the glove object looks in the Unity inspector.
It is very important to write the name of Unity callback methods correctly, otherwise Unity will not be able to detect them on the object (and as a result, can never execute them).
In your case, you have misspelled the OnCollisionEnter callback.
Instead of OnCollisonEnter it should be OnCollisionEnter.
So I have 2 objects on my imageTarget and i added a box collider to both of them. I also added a different tag to them. I want it so that if you click on one of them it brings you to a different scene with info about that object.
This is my code:
using System.Collections;using System.Collections.Generic;
using UnityEngine;
using Vuforia;
using System.IO;
public class ObjectInfo : MonoBehaviour
{
public GameObject Eagle;
public GameObject LibertyStatue;
// Use this for initialization
void Start ()
{
Eagle = GameObject.Find ("Eagle");
LibertyStatue= GameObject.Find ("LibertyStatue");
//Eagle.SetActive (true);
//LibertyStatue.SetActive (true);
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if(hit.collider.tag == "Eagle");
{
ChangeScene ("EagleInfoScene");
}
if(hit.collider.tag == "LibertyStatue");
{
ChangeScene ("LibertyStatueInfoScene");
}
}
}
}
public void ChangeScene (string a)
{
Application.LoadLevel (a);
}
}
When i first added only one of the objects it worked fine, now after adding a new object and a new scene, both objects change to the new scene. So clicking both the eagle and liberty statue changes the scene to LibertyStatueInfoScene. Is there a way to fix this?
Solved: Wow im really stupid, the issue was that i had semicolons after my if statements, it didn't give me an error so i never noticed it.
Solution: I didnt notice the semicolons after the if statements, removing them solves it. Strangely it didn't give me an error.
I'm having a bit of trouble getting the Vector3 wayPointPosition to my other script called Walking and changing it into the Transform target. My troubles lie in the fact that I'm trying to grab this dynamic variable from WayPointPositioner (it changes depending on what object is clicked in the stage and whether the player overlaps with this waypoint) and import and use it in another script.
Below is the code I'm using.
WayPointPositioner
using UnityEngine;
using System.Collections;
public class WayPointPositioner : MonoBehaviour {
public Vector3 wayPointPosition = Vector3.zero;
private bool checkPlayerWaypointCollision;
void Start()
{
}
void OnTriggerStay2D (Collider2D other)
{
// Check if collision is occuring with player character.
if (other.gameObject.name == "Player")
{
checkPlayerWaypointCollision = true;
}
else
{
checkPlayerWaypointCollision = false;
}
}
//Check if object is clicked
void OnMouseDown ()
{
// If its the player, then return a new position for the player to move to for walking
// Else debug that its not so
if (checkPlayerWaypointCollision == false)
{
Debug.Log ("Object not colliding and retrieving position");
Debug.Log (wayPointPosition);
Debug.Log (gameObject.name);
wayPointPosition = new Vector3 (transform.position.x, transform.position.y, 10);
wayPointPosition = Camera.main.ScreenToWorldPoint(wayPointPosition);
}
else
{
Debug.Log ("Object is colliding, no movement needed");
}
}
}
Walking
using UnityEngine;
using System.Collections;
public class Walking : MonoBehaviour {
public Transform target;
public WayPointPositioner wayPointPosition;
public bool walkingAnimation = false;
private Animator anim;
void Awake ()
{
anim = GetComponent<Animator> ();
wayPointPosition = GameObject.FindGameObjectWithTag ("Waypoint").GetComponent<WayPointPositioner> ();
}
void Start ()
{
}
void Update ()
{
Debug.Log ("This is in Walking, WPP =" + wayPointPosition);
}
}
As you can see I'm trying to import the wayPointPosition from the seperate class which is attached to the gameobjects called "Waypoint" (In my current layout those are empty objects with circle colliders to check if they have been clicked). However when I run this, I am not getting my Vector, but I'm getting the name of the last waypoint in the hierarchy (I have currently 6 waypoints which can be clicked) and not a Vector.
I hope someone is able to help me / point out my mistake. I'm still learning C# so I might've made a strange / odd assumption which isn't working.
Kind regards,
Veraduxxz.
It looks like invoking GameObject.FindGameObjectWithTag("Waypoint").GetComponent<WayPointPositioner>(); retrieves a component from the game object which matches the specified tag, as well as a type argument T which derives from MonoBehavior.
Calling this should actually give you an instance of your WayPointPositioner class, which you can then pass to whichever methods you want, and interact with its Vector3 however you would like.