- Home /
Question by
bruu · Jan 28, 2014 at 05:43 PM ·
platformerwindconstantforce
Using ConstantForce to simulate wind
Hello, I'm trying to create a simple platformer. I have triggers set around where if the player collides, ConstantForce is enabled on the player, and the player is pushed in a chosen direction (depending on the trigger). This works relatively well with pushing the player left and right, but where I'm having an issue is when the player is pushed up on the Y axis. The player hovers for a bit but each time he drops, he begins to go lower and lower- eventually exiting the trigger. I want him to maintain a certain height in air unless he Exits the trigger. Increasing ConstantForce only makes him fly higher, decreasing his mass has a similar effect.
using System;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using System.Collections;
public class WindTrigger : MonoBehaviour
{
private ConstantForce getConstantForce;
public GameObject findingThePlayer;
public Vector3 forceDirection = new Vector3();
void Start()
{
//Getting the ConstantForce component from player
findingThePlayer = GameObject.Find("pig_test");
getConstantForce = findingThePlayer.GetComponent<ConstantForce>();
}
void OnTriggerStay()
{
//turning on the players ConstantForce
getConstantForce.enabled = true;
//we set the vector value in editor, depending which way we want the wind to blow
getConstantForce.constantForce.force = forceDirection;
}
void OnTriggerExit()
{
//When player exits trigger, ConstantForce is disabled
getConstantForce.enabled = false;
}
}
Comment