#Pogo Help 1032021
1 messages · Page 1 of 1 (latest)
give me a second
Remember to post it here by using three `'s
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//variable you can change in Inspector
public float sidewaysForce = 400f;
//makes player's rigidbody "rb"
public Rigidbody rb;
void Update()
{
//right movement
if (Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
//left movement
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
}
it adds a force to the rigidbody if u press a or d (i made a variable for that that I can change in inspector)
and then did all the Time.delta stuff that I don't really understand
Sorry for waiting
np
Hmm...
i think a slippery floor would fix it, but more problems would come up later
not sure how to do player movement any differently though
I'm trying not to just ctrl+c and ctrl+v
It's fine to use physics, but there are a few things you need to remember:
add forces in FixedUpdate, as that is when unity simulates physics.
Input shout still be in regular update.
And you don't need to multiply force by delta time. You could(considering it's in fixed update, deltaTime = fixedDeltaTime), but don't have to.
Oh wait, you want physics
Yes. Any kind of physics manipulations should be in FixedUpdate.(not really, but as a beginner you should remember that first)
give me a sec
IN the rigidbody settings, click freezeRotation.x
What was the problem anyways?
Also z, then re-enable it if you wanna do like Ragdolls
can you have functions in functions?
No, you can't
Technically you can, but you really shouldn't have a reason to do that as a beginner.
it's called local functions.
i am very confused how i add the forces in FixedUpdate but inputs in normal Update
You cache the input values in update as a class variable and use it in fixed update.
I am so confused
I am sorry
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//variable you can change in Inspector
public float sidewaysForce = 0f;
//makes player's rigidbody "rb"
public Rigidbody rb;
void Update()
{
//if d is pressed, ---
if (Input.GetKey("d"))
{
moveRight();
}
//if a is pressed, ---
if (Input.GetKey("a"))
{
moveLeft();
}
}
void FixedUpdate()
{
}
}
i got this far
I think thats what you mean?
I dont really know the technical terms
I'm not sure what moveRight and moveLeft are. And you don't have anything in fixed update. You could just move the AddForce calls to FixedUpdate as they are.
I thought like
idk
but how do I link the "if _ is pressed" in the update to the AddForces
There are a few ways, but think logically. What type do you check in an if statement?
I cant think logically
Let's say Input.GetKey("d"). What does it return?
Okay, what variable type can hold these values?
bools?
That's what we refer to when we say "cache something" usually.
https://help.vertx.xyz/?page=programming/common-input-issues/input-in-fixed-update example.
(Though you should be able to use Input.GetKey in fixedupdate because it's a continuous state-based query and not an instantaneous input it's probably advisable to keep all input logic in Update for consistency's sake)