- Home /
Walls not moving in
For my game I need the outer walls to close in towards the center, but for some reason they just hop forward into a close position instantly, then all collide in the center and take off in random directions. Help?
using UnityEngine;
using System.Collections;
public class ApproachWalls : MonoBehaviour {
public Vector3 pos;
public Vector3 newPos;
public Vector3 differenceOne = new Vector3(0,0,5);
public Vector3 differenceTwo = new Vector3(0,0,5);
public Vector3 differenceThree = new Vector3(5,0,5);
public Vector3 differenceFour = new Vector3(-5,0,5);
// Use this for initialization
void Start () {
pos = transform.position;
InvokeRepeating ("closeIn", 5.0f, 5.0f);
}
// Update is called once per frame
void Update () {
}
void closeIn (){
if (transform.position.z < 0 && transform.position.x == 0 && transform.position.z !=0) {
newPos += differenceOne;
rigidbody.MovePosition(newPos);
} else if (transform.position.z > 0 && transform.position.x == 0 && transform.position.z !=0) {
newPos += differenceTwo;
rigidbody.MovePosition(newPos);
} else if (transform.position.x < 0 && transform.position.z == 0 && transform.position.x !=0) {
newPos += differenceThree;
rigidbody.MovePosition(newPos);
} else if (transform.position.x > 0 && transform.position.z == 0 && transform.position.x !=0) {
newPos += differenceFour;
rigidbody.MovePosition(newPos);
}
}
void OnCollisionEnter (Collision col){
Physics.IgnoreCollision(col.collider, collider);
}
}
could you not just use animation using the animator rather than trying to do it using a script?
Answer by ROLLERROCK · May 01, 2015 at 03:45 PM
if you can predefine the final and initial positions, you could use the Lerp function. (the tutorial for lerp is in the video tutorials offered by unity)
Your answer
Follow this Question
Related Questions
After parent change, rigidbody falls through terrain 0 Answers
Colliding with Terrain and Destroying it 1 Answer
How to make a deform on a terrain with the points on a collision of rigidbody 0 Answers
Check if a rigidbody has stopped moving for several frames. 2 Answers
blending meshes with terrain 0 Answers