Hello guys,
Currently I’m trying to make a FPS game with a faux gravity. I set my faux gravity for my player and the ground with a body and attractor and when I started doing shooting I noticed that the bullets were all flying towards sky. I couldn’t understand why it is so first but I guess it is because of the transform.position changes in parents.
What can I do in order to stop the child to get effected by parent? Here are the codes
Gravity Body
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
public class GravityBody : MonoBehaviour {
GravityAttractor planet;
Rigidbody rigidbody;
void Awake () {
planet = GameObject.FindGameObjectWithTag("Ground").GetComponent<GravityAttractor>();
rigidbody = GetComponent<Rigidbody> ();
// Disable rigidbody gravity and rotation as this is simulated in GravityAttractor script
rigidbody.useGravity = false;
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
void FixedUpdate () {
// Allow this body to be influenced by planet's gravity
planet.Attract(rigidbody);
}
}
Gravity Attractor
using UnityEngine;
using System.Collections;
public class GravityAttractor : MonoBehaviour {
public float gravity = -100f;
public void Attract(Rigidbody body) {
Vector3 gravityUp = (body.position - transform.position).normalized;
Vector3 localUp = body.transform.up;
// Apply downwards gravity to body
body.AddForce(gravityUp * gravity);
// Allign bodies up axis with the centre of planet
body.rotation = Quaternion.FromToRotation(localUp,gravityUp) * body.rotation;
}
}
Fire
using UnityEngine;
using System.Collections;
public class Fire : MonoBehaviour {
public Rigidbody projectile;
public float speed = 1;
public Transform weapon;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate(projectile,
transform.position,
weapon.rotation)
as Rigidbody;
Debug.Log(weapon.position);
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
//instantiatedProjectile.AddForce(instantiatedProjectile.transform.forward * speed);
}
}
}