- Home /
How can I make an object move up on collision?
How do I move an object downward using the Vector() command? I want to implement a door opening by it sinking into the ground, when the player collides with a button on the ground. I have experimented with this, but nothing seems to work. I tried animation, but the animation has to be done for each door, and sometimes does not work at all. How would I write a simple Vector() script that would move a GameObject up a certain number of Unity units, with the amount set as a variable?
-Thanks
I don't just want code, I want to know how to do this...
this is very urgent, I need this done by sunday
Answer by sparkzbarca · Mar 15, 2013 at 02:13 AM
You want lerp or slerp
they use math to interpolate from one position to the next
interpolate basically means to go from one position to another position by means of moving really small amounts.
if you'd like i can give you more info about interpolating. Post a request.
Mark as asnwerd
//script attached to door
void update()
{
public bool LowerDoor = false;
float FinalHeight = 0f;
vector3 FinalPosition = new vector3 (transform.position.x, FinalHeight, Time.deltatime);
if(LowerDoor)
door.trasform.position = vector3.lerp(door.transform.position, FinalPosition);
}
change the public lowerdoor bool in the collision script.
Slight problem with this code- I am getting two compiler errors when I run this... Can you try and resolve these and repost? I suck at debugging, and I always feel like a dumbass (excuse language) after I find out the problem....
much appreciated!
-Thanks
hehe well yea it was rough code but i'm feeling generous so i'll code it up for ya
attach this to the door.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : $$anonymous$$onoBehaviour {
public bool LowerDoor;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
LowerDoor = false;
float FinalHeight = 0f;
Vector3 FinalPosition = new Vector3 (transform.position.x, FinalHeight, transform.position.z);
if(LowerDoor)
transform.position = Vector3.Lerp(transform.position, FinalPosition, Time.deltaTime);
}
}
attach this to the button
make it a new script named OpenDoor
using UnityEngine;
using System.Collections;
public class OpenDoor : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collision player)
{
player.gameObject.GetComponent<NewBehaviorScript>().LowerDoor = false;
}
}
Thumbs up, because someone has to give credit where it's due... =___=