- Home /
My Planet Gravity system not working? C#
I have a system setup to try and detect when a player is in a specific planets gravity, but for some reason its not working. I will have my code below I have empty game objects with mesh colliders and rigidbodys on them to emulate the atmosphere and when they enter a certain one, they need to be pulled in. Here is the code i have: Thank you
using UnityEngine; using System.Collections;
[RequireComponent (typeof (Rigidbody))] public class GravityBody : MonoBehaviour {
PlanetGravityEnter planet;
void Awake ()
{
planet = GameObject.FindGameObjectWithTag("Planet").GetComponentInChildren<PlanetGravityEnter>();
rigidbody.useGravity = false;
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
void FixedUpdate ()
{
planet.Attract (transform);
}
}
using UnityEngine; using System.Collections;
public class PlanetGravityEnter : MonoBehaviour {
public float gravity = -10f;
public void OnTriggerStay (Collider body)
{
Attract (transform);
}
public void Attract(Transform body)
{
Vector3 targetDir = (body.position - transform.position).normalized;
Vector3 bodyUp = body.up;
body.rotation = Quaternion.FromToRotation (bodyUp, targetDir) * body.rotation;
body.rigidbody.AddForce (targetDir * gravity);
}
}
using UnityEngine; using System.Collections;
public class FirstPersonController : MonoBehaviour {
public float mouseSensitivityX = 250f;
public float mouseSensitivityY = 250f;
public float walkSpeed;
public float runSpeed;
public float jumpForce;
public LayerMask groundedMask;
Transform cameraT;
float verticalLookRotation;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
bool grounded;
bool doubleJump = false;
void Start ()
{
Screen.showCursor = false;
cameraT = Camera.main.transform;
}
void Update ()
{
transform.Rotate (Vector3.up * Input.GetAxis ("Mouse X") * Time.deltaTime * mouseSensitivityX);
verticalLookRotation += Input.GetAxis("Mouse Y") * Time.deltaTime * mouseSensitivityY;
verticalLookRotation = Mathf.Clamp (verticalLookRotation, -80, 80);
cameraT.localEulerAngles = Vector3.left * verticalLookRotation;
Vector3 moveDir = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical")).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
if (Input.GetKey(KeyCode.LeftShift) && grounded)
{
targetMoveAmount = moveDir * runSpeed;
}
moveAmount = Vector3.SmoothDamp (moveAmount, targetMoveAmount, ref smoothMoveVelocity, 0.15f);
if (Input.GetButtonDown ("Jump") && (grounded || !doubleJump)){
{
rigidbody.AddForce(transform.up * jumpForce);
if(!doubleJump && !grounded)
doubleJump = true;
}
}
if (grounded)
doubleJump = false;
grounded = false;
Ray ray = new Ray (transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 1 + 0.1f, groundedMask))
{
grounded = true;
}
}
void FixedUpdate()
{
rigidbody.MovePosition (rigidbody.position + transform.TransformDirection (moveAmount) * Time.fixedDeltaTime);
}
}
what not working ? is it not attracting game object ? or can you mention where is problem in code?
Why do so few people write comments in their code :( it's hard to understand other peoples code if there are no comments.
Anyway I'm not entirely sure how you are trying to accomplish what I think you want but since it seems fairly simple I'll just write some code on how I'd do it...
using UnityEngine;
using System.Collections.Generic;
public class Attract : $$anonymous$$onoBehaviour {
public List<Rigidbody> inGravityWell = new List<Rigidbody>();
public float gravity;
void OnTriggerEnter(Collider c){
inGravityWell.Add(c.rigidbody);
}
void OnTriggerExit(Collider c){
inGravityWell.Remove(c.rigidbody);
}
void FixedUpdate(){
for(int i = 0; i < inGravityWell.Count; i++){
Vector3 force = (transform.position-inGravityWell[i].position).normalized * gravity;
inGravityWell[i].AddForce(force, Force$$anonymous$$ode.Force);
}
}
}
No guarantees that this works, I just wrote it without any testing, but attaching that script to any planet should make it pull in any rigidbodies that enter it's trigger area.
The code does not work, what do i need to do, such as do i need to modify my other scripts?
Does the code not do what you expect or does it generate errors? some things that could be going wrong with what I wrote: 1) OnTriggerEnter is not called. Are rigidbodies added to the list?
2) The gravity is too low to have any noticable effect.
3) I should have used a different Force$$anonymous$$ode.
I have the script :
using UnityEngine; using System.Collections.Generic;
public class Attractor : $$anonymous$$onoBehaviour {
public List <Rigidbody> inGravityWell = new List <Rigidbody> ();
public float gravity = -10f;
void OnTriggerEnter (Collider c){
inGravityWell.Add (c.rigidbody);
}
void OnTriggerExit (Collider c){
inGravityWell.Remove (c.rigidbody);
}
void FixedUpdate(){
for (int i = 0; 1 < inGravityWell.Count; i++) {
Vector3 force = (transform.position - inGravityWell [i].position).normalized * gravity;
inGravityWell [i].AddForce(force, Force$$anonymous$$ode.Force);
}
}
}
and:
using UnityEngine; using System.Collections;
public class Body : $$anonymous$$onoBehaviour {
PlanetGravityEnter planet;
void Awake ()
{
planet = GameObject.FindGameObjectWithTag("Planet").GetComponentInChildren<PlanetGravityEnter>();
rigidbody.useGravity = false;
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
void FixedUpdate ()
{
planet.Attract (transform);
}
}
Answer by MonkeyHood · Feb 20, 2015 at 03:13 PM
public void Attract(Transform body)
{
Vector3 targetDir = (body.position - transform.position).normalized;
Vector3 bodyUp = body.up;
body.rotation = Quaternion.FromToRotation (bodyUp, targetDir) * body.rotation;
body.rigidbody.AddForce (targetDir * gravity);
}
I think your problem is in your target dir. To correctly generate direction vectors, the calculation is heads - tails or (destination - currentPosition). If you want to attract towards the planet, you must do
Vector3 targetDir = (transform.position - body.position).normalized;
Your answer
Follow this Question
Related Questions
Mesh Collider Does Not Working 1 Answer
Apply gravity to parents 1 Answer
Implementing FPS view into locomotion planet walk 1 Answer
First Person Controler and Water wont effect gravity 1 Answer
faux gravity in 2d game 3 Answers