- Home /
First Person Controler and Water wont effect gravity
I'm using the standard asset First Person Controller. I set up some water and made the water a cube and made that collider a trigger. I want to make it so when the character falls down into the water there is no gravity so he can start swimming. But when I use
void OnTriggerEnter(Collider other) { other.attachedRigidbody.useGravity = false; }
nothing happens, the gravity doesn't stop but if i do just
OnTriggerEnter(Collider other)
{
Destroy(GameObject);
}
then when the character hits the water it works and he gets deleted. So I don't know why the gravity wont turn off in the first example. The only thing I can think of is all the scripts on the First Person Controller are in Java, and my scrips are in C#. Are java and C# compatible together in unity?
Answer by ScroodgeM · Jul 30, 2012 at 08:44 PM
attach this to object with trigger that simulates water. on enter to trigger character controller's speed is set to zero and gravity is disabled. on exit gravity is enabled again.
using UnityEngine; using System.Collections;
public class WaterZone : MonoBehaviour { void OnTriggerEnter(Collider other) { CharacterMotor cm = other.gameObject.GetComponent(); cm.SetVelocity(Vector3.zero); cm.movement.gravity = 0f; } void OnTriggerExit(Collider other) { CharacterMotor cm = other.gameObject.GetComponent(); cm.movement.gravity = 10f; } }
Yeah I do have a NullReferenceException WaterNoGravity.OnTriggerEnter on the line for useGravity = false .
I'm pretty new to program$$anonymous$$g and I've never used triggers before. I don't understand how this should connect to the player.
rather you have no rigidbody attached to collider you get in method. insert 'Debug.Log(other.gameObject.name)' to be sure you hangle correct object.
First Person Controller UnityEngine.Debug:Log(Object) WaterNoGravity:OnTriggerEnter(Collider) (at Assets/Assets/Scripts/WaterNoGravity.cs:8)
just found that First Person Controller doesn't use a rigidbody. so you need something like
void OnTriggerEnter(Collider other) { CharacterController cc = other.gameObject.GetComponent<CharacterController>(); cc.velocity = Vector3.zero; Character$$anonymous$$otor cm = other.gameObject.GetComponent<Character$$anonymous$$otor>(); cm.Gravity = 0; }
Yeah that's what I thought actually, I was looking at the FirstPersonController front to back and could not find a rigidbody anywhere. CharacterController is read only, and Character$$anonymous$$otor doesnt have a gravity field but ill look into it.
Your answer
Follow this Question
Related Questions
Changing the objects rather than time. 2 Answers
How reliable are isGrounded checks? 1 Answer
Low gravity on trigger. 2 Answers
Trigger with Gravity + Collision 2 Answers
Trigger gravity by external collider 2 Answers