- Home /
Trigger enter, move an object to x,y and z co-ordinates.
Hey everyone. I have a scene set up with a player using the fps controller. I also have a box, with no mesh renderer, and trigger set to on. I would like a snippet of code that when the player walks inside the cube trigger, that it moves another object in the scene into view of the player. I want to be able to make the player walk up to a log, and a plane textured with info will rise up for the player to read, and dissapear when they walk away.
Please help as this is urgent and i can't script.
If it's urgent, then maybe you should've learned how to script before beginning on the project!
:| I was the designer and artist on a $$anonymous$$m, until the programmer left. So I need to finish a few little things like this myself.
This used to happen to me all the time, I understand. Later, I learned how to code, so I'd never have to put up with unreliable programmers ever again! You should too, it's a useful skill these days.
Answer by syclamoth · Dec 01, 2011 at 10:57 AM
public Transform sign;
public Vector3 signTargetOffset;
public float raiseTime = 2;
Vector3 signStart;
Vector3 signTarget;
public GameObject player;
void Start()
{
signStart = sign.position;
signTarget = signStart + signTargetOffset;
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject == player)
{
StartCoroutine(RaiseSign(raiseTime));
}
}
IEnumerator RaiseSign(float time)
{
float elapsedTime = 0;
while(elapsedTime < time)
{
sign.position = Vector3.Lerp(signStart, signTarget, elapsedTime / time);
elapsedTime += Time.deltaTime;
yield return null;
}
sign.position = signTarget;
}
Put this on your trigger volume, then drag the gameObjects for the sign, and the player onto the view the script gives you in the inspector, and tell it how far the sign should move when it rises, and how long it should take.
It's in C#. You really do have a lot to learn. Don't just copy it into the head of the file- it needs to go inside the class, under the line
public class YourScript : $$anonymous$$onoBehaviour {
and so on. Any functionality needs to happen inside the curly brackets.
Would it be easier to make the panel invisible, unless the player is within the collision area?
Not really, but you can do that if you want to.
void OnTriggerEnter(Collider other)
{
if(other == player)
{
sign.renderer.enabled = true;
}
}
void OnTriggerExit(Collider other)
{
if(other == player)
{
sign.renderer.enabled = false;
}
}
I'm sorry, I am a complete noob here. I can not get either of these scripts working.
Answer by Allrj · Sep 14, 2016 at 12:30 PM
@syclamoth I'm really hoping you are still active on the unity store because i am desperate, i have all your code entered (Had to type it in, copy and paste wouldn't work xD) and i have all the options in the inspector but it just doesn't do anything when my character collides with the trigger :/ idk what to do. (also relatively new to scripting)
my script:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public Transform sign;
public Vector3 signTargetOffset;
public float raisetime = 2;
Vector3 signStart;
Vector3 signTarget;
public GameObject player;
void Start()
{
signStart = sign.position;
signTarget = signStart + signTargetOffset;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player) {
StartCoroutine (RaiseSign (raisetime));
}
}
IEnumerator RaiseSign(float time)
{
float elapsedTime = 0;
while (elapsedTime < time) {
sign.position = Vector3.Lerp (signStart, signTarget, elapsedTime / time);
elapsedTime += Time.deltaTime;
yield return null;
}
sign.position = signTarget;
}
}
Your answer
Follow this Question
Related Questions
moving my wall up & down 2 Answers
Trigger problem 2 Answers
Can't figure out how to achieve an effect like QUBE 1 Answer
Object moving OnTriggerEnter, please help?! 2 Answers
How would I go about moving a group of objects as one using iTween? 1 Answer