- Home /
Question by
Wesley21spelde · Feb 14, 2016 at 11:08 AM ·
scripting problemtriggertimegravity
time dilation & gravity Effect Not working propperly
Hey guys i tried to make a time dilation & gravity Effect. The Effect works Verry Simple but works the only problem is that enything is affected by the effect. and it should be only Objects within the Field(Trigger) How can i fix this
also if somone knows a better way of doing this. or for a better effects please let me know thanks
using UnityEngine;
using System.Collections;
public class LowGravityZone : MonoBehaviour {
// Use this for initialization
void Start () {
Physics.gravity = new Vector3(0, -10.0F, 0);
Time.timeScale = 1.0F;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
Physics.gravity = new Vector3(0, -1.0F, 0);
Time.timeScale = 0.07F;
}
void OnTriggerExit(Collider other)
{
Physics.gravity = new Vector3(0, -10.0F, 0);
Time.timeScale = 1.0F;
}
}
Comment
Answer by WillNode · Feb 14, 2016 at 04:28 PM
You can do that by put a custom script for manage the acceleration of gravity on each object. simply attach this to your object:
[RequireComponent(typeof(Rigidbody))]
public class CustomGravity : MonoBehaviour
{
public Vector3 gravity;
public float timeScale = 1;
Rigidbody Body;
void Start ()
{
Body = GetComponent<Rigidbody>();
Body.useGravity = false;
}
void FixedUpdate()
{
Body.velocity += gravity * Time.fixedDeltaTime * timeScale;
}
public void ChangeTimeScale(float now)
{
//The tricky part: simply rescale the velocity
//But try never put the value to zero.
Body.velocity *= now/timeScale;
timeScale = now;
}
}
and for your LowGravityZone is done like this:
public class LowGravityZone : MonoBehaviour {
void Start () {
}
void OnTriggerEnter(Collider other)
{
CustomGravity cg =
other.gameObject.GetComponent<CustomGravity>();
//It's not clear whether you want to change the gravity
//since you also slows down the time.
cg.gravity = new Vector3(0, -1.0F, 0);
cg.ChangeTimeScale(0.07F);
}
void OnTriggerExit(Collider other)
{
CustomGravity cg =
other.gameObject.GetComponent<CustomGravity>();
cg.gravity = new Vector3(0, -10.0F, 0);
cg.ChangeTimeScale(1F);
}
}
Your answer
Follow this Question
Related Questions
How to disable player gravity 2 Answers
C# | How to disable gravity for a certain amount of time? 1 Answer
Animation Problems 1 Answer
Animation Trigger is saved even if it is not used immediately. 1 Answer
Collison detection not working? 1 Answer