I really really want some realistic physics in my simple side scroller space game but I can’t find a tutorial that goes deep from start to finish about this.
I’m not a programmer so making sense of everything in c# is not always happening for me. But I’m not discouraged. Here’s what I have so far:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, yMin, yMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (-moveHorizontal, moveVertical, 0.0f);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp (rigidbody.position.y, boundary.yMin, boundary.yMax),
0.0f
);
}
}
I’ve defined boundaries for my spaceship, and it can move. But the background isn’t moving and the movement of the spaceship is too stiff/arcade like. I want it to keep moving for a little while after releasing the keyboard key and I want the camera to follow the ship and the background to move only when the ship moves.
Can anyone help me with that? Either with scripting or a link to a tutorial that shows what I’m talking about, would be great
Yeah, you should post this in support. But also, Answers is a much better tool for finding help.
Any way, BFGames is right. Force is the way to go. Think of force as a nudge. You have a ball then you add force in a direction, or nudge it, for a single frame and it will travel in that direction. If you have it continuously applied it will move at a constant rate until it is no longer applied, then the object will slow to a stop.
Also, add force will make sure that your collisions are detected properly.
The only reason you’d want to do it different is if you want to work with your own physics, which people often do to save on CPU. You should be alright though, so long as you don’t have thousands of objects using force at once.
I just made a new post in support. Thanks for your help. Im so new to programming that I don’t even know where in my code I should put the add force code. I’ve tried putting it in the fixed update somewhere underneath but I can’t get it to work. Where would you put the :
void FixedUpdate() {
rigidbody.AddForce(Vector3.up * 10);
}
And is it the only lines of code I need to put in my player controller script?
Before the fixed update and it says in the error message: Input key named “UpArrow” is unknown.
This may be because I haven’t added : void AddForce(float x, float y, float z, ForceMode mode = ForceMode.Force); Do I have to make a void AddForce to define it before I can add the void update? Please excuse me if thats a stupid question.
I’m learning a lot here I really appreciate your help!
Alright so here’s what I’ve tried highlighted in blue in the code beneath:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, yMin, yMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public Boundary boundary;
[COLOR=#0000ff]void Update() {
if (Input.GetKeyDown("Up")){
rigidbody.AddForce(Vector3.up * 10);
}[/COLOR]
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (-moveHorizontal, moveVertical, 0.0f);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp (rigidbody.position.y, boundary.yMin, boundary.yMax),
0.0f
);
}
}
I also tried as BFGames said to add it in the fixed update. But wether I add just the rigidbody.AddForce(Vector3.up * 10); or the entire
{
if (Input.GetKeyDown(“Up”)){
rigidbody.AddForce(Vector3.up * 10);
}
it doesn’t work.
This is what it looks like in the fixed update, when i play this I can’t move the ship at all, is it because I have conflicting code in regards to movement perhaps?
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, yMin, yMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public Boundary boundary;
void FixedUpdate ()
{
[COLOR=#4d4dff] if (Input.GetKeyDown("Up")){
rigidbody.AddForce(Vector3.up * 10); [/COLOR]
[COLOR=#4d4dff]}[/COLOR]
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (-moveHorizontal, moveVertical, 0.0f);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp (rigidbody.position.y, boundary.yMin, boundary.yMax),
0.0f
);
}
}
update: If I only add the line rigidbody.AddForce(Vector3.up * 10); in the fixed update I don’t get compiler errors and can fly the ship but theres no difference from the original movement. It doesn’t affect the ship at all. Man I’m confused…
This works really well. Now just gotta figure out how to get my Ellipsoid prefab small flame to thrust when I hold down a key I’ve got code that works for the regular particle system but not the ellipsoid emitter for some resason.
Anyway thanks for the help guys