Hey everyone! I’m a bit new to the concept of developing games in a 3D environment so as you can about imagine… Physics got me a bit frazzled here. Getting to the point here, I have a game I’m developing called GravityShift. You play as a rolling sphere in which rolls along various platforms in all different directions and angles. Here’s the catch… each time you touch a new platform… your pull of gravity switches to that of the platforms. So… if you rolled onto the left wall, that becomes your new floor and gravity will adapt to such. Hopefully… this makes sense. I have a base code similar to Faux Gravity developed… and it does work… EXCEPT… the Player gets pulled to the center of the platform(I know exactly why just unsure of a proper way of doing so yet…) as the RigidBody will have a force applied to it when the distance between it’s position and the platforms position exceed the Gravity Radius. This would be ideal for a round object… but a flat pane… not so much. Is there a better way to be doing this?? Any help would be greatly appreciated, as physics have always scared me from doing 3D development slightly.
Thank you everyone! Code is posted below the video! EDIT: added youtube video of a game with the basic concept of what I’m attempting to accomplish. The rest of the modifications can be done from the base… I just can’t get the physics down :(*
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public Transform platformTarget;
public float forceAmount = 1000.0f;
public float gravityRadius = 10.0f;
Vector3 targetDirection;
public float speed;
public Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
//Physics.gravity = Vector3.zero;
}
void Update ()
{
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
float distance = Vector3.Distance (transform.position, platformTarget.position);
targetDirection = platformTarget.position - transform.position;
targetDirection = targetDirection.normalized;
if (distance < gravityRadius) {
rb.AddForce (targetDirection * forceAmount * Time.deltaTime);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Platform"))
{
platformTarget = other.gameObject.transform;
}
}
}
Thing is, the platforms can be in all different directions and angles. I need gravity to accustom to each platform ONLY on contact. Otherwise the old gravity still applies. Adds a difficulty factor. Although, I’ll take a look and see if that can do anything. Thanks!
EDIT: Yeah, I used a similar method. When the Player Collides with a “Platform” it automatically shifts. I managed that far(code states what it will and won’t do) but yes… I have the shifting down BUT the gravity pulls you to the center of the object by default like a Flat Earth type deal.
I think the suggestion to use the opposite of the normal for gravity sounds pretty good. Did you try that?
If I were in your shoes, trying to build that game, I might just forget about having gravity turned on, and simulate gravity on my own, as you said. it updates with every touched surface, but remains the same until a new surface is touched. That sounds reasonable.
People don’t seem to understand I never was using their gravity. Revering is was the first thing I tried, and I’m unsure if people are even viewing my code but it should clearly show what was attempted. It’s pulling it towards the orgin point of the platform by default because I literally have no understanding of physics and how to not make it do that… I had to Google Velocity and still don’t know what it is. It calculates the distance between the player and the platformTarget’s positions and exerts a force on the player until it reaches there. I’m going off total fluke math and was hoping someone with legitimate understanding of how the physics engine in unity works could better explain… because the tutorial’s are not teaching me anything in terms of these types of physics. I never intended on using their gravity. ALSO, as seen in the video… gravity can need to be applied in all different directions and angles… not just UP and DOWN. I’ve tried Raycasting to determine calculations with no prevail. I’m literally coding blind math I find on the internet.
Sorry, first thing… Almost no one will or wants to download a file to read code. The easiest option is to post your code right here in the thread! Please refer to this page for how to do it properly, so it comes formatted and easy to read: Using code tags properly
That means, at least for me, no sorry I never read your code and never saw what you tried.
From your explanation, though, if I’m reading it correctly, you are contradicting yourself a little bit, by saying that reversing the “normal” of the plane is the first thing you tried, then later saying you have no idea what you’re doing in a number of areas; that seems a bit suspect.
The idea of the game looks fun, cool, and interesting. I don’t mean to sound like a downer to your hopes here, but in all honesty, I think you would be infinitely better off by doing some easier tutorials to get better acquainted with Unity, physics, and whatever else you think you’ll be needing.
I honestly had no idea if it was allowed to post code or not in topic, as alot of forums get mad for that. I… don’t know anything about physics obviously… but I mean’t… I obviously should know as any human being how to reverse a variable. Literally… almost anyone I’d hope would know how to reverse something… just it didn’t work for what I wanted. The code I provided is much too long for a forum post in my opinion… and felt I was going to get yelled at for posting it. It does EVERYTHING I want except it has a ‘flat earth’ effect of pulling you to the center of the given platform. Otherwise, as it stands it… is 90% of what I want to accomplish.
Okay, well for future readers to this thread, who may be able to help you, if you could post the relevant portion of your code, that would go a long way.
I’m fresh out of ideas; perhaps some other readers can offer better advice. I do hope you figure it out.
Yeah… I’ll do that thank you. I honestly am too if I’m gonna be honest!! I’ll redo the post to make better sense. I really hope I can bring this game to commission. Thanks for trying guys!
Here, I thought I would try to do something as an example.
There’s nothing fancy like “moving” (hahah), but I made you a small package.
When you open it, just use the mouse to click on 1 of the 4 walls. Then, use spacebar to jump.
Obviously this is not really a game , but maybe it will be a bit helpful
Thank you soooo much, with help from your project and Raycasting… and a bit of messing around in the movement system. I GOT IT TO WORK 100% I have honestly never been so relieved in my life! Thank you!