#ThirdPersonMovement
1 messages · Page 1 of 1 (latest)
Explain again what you are trying to achieve, how you are trying to achieve it, and what is happening instead.
Post any Errors.
If there's a tutorial involved, post link and any relevant timestamps.
It's based off of a tutorial. I've no clue as to why it doesn't work. The only part that isn't working is the part that detects how far my player has fallen (So that they can apply damage if they fall a certain height)
Alright. Link the tutorial.
Quick little video on how to fall damage in Unity.
Thanks for watching!
Chapters
0:00 Intro
0:08 The preparations
1:40 Detecting when we "land"
2:40 Telling how far we fell
5:31 Implementing a minimum fall distance
7:23 So yeah...
7:49 Outro and Patreon thanks
----------------------------------------------------------------------------------...
One thing is that they're using a rigidbody instead of a charactercontroller, but they don't reference it in their scripts.
Alright. So your movement script is working fine, and now you're trying to add fall damage, by example of this tutorial.
Yes.
I forgot to add
void Damage()
{
float fallDistance = startOfFall - transform.position.y;
if (fallDistance > minimumFall)
{
hp.HeartCount -= 1;
Debug.Log(transform.position.y);
}
}
In the code snippet I sent.
The "if falldistance > minimumFall"
minimumFall is the distance my player has to fall to take damage
Alright.
So the first code link is the movement script alone?
I sent the relevant code using inline tags, the entire script is inside the link
private void FixedUpdate()
{
if (!wasFalling && isFalling)
{
startOfFall = transform.position.y;
}
if (!wasGrounded && controller.isGrounded)
{
Damage();
}
wasFalling = isFalling;
wasGrounded = controller.isGrounded;
}
void Damage()
{
float fallDistance = startOfFall - transform.position.y;
if (fallDistance > minimumFall)
{
hp.HeartCount -= 1;
Debug.Log("Player Fell " + (startOfFall - transform.position.y) + " units");
}
}
This is very important: I need an exact copy of the script at hand.
Ope, wait a second. I forgot to remove some debugging stuff.
exact means exact - it's important that I know what you're sitting with, because we are in fact looking for some type of mistake.
anyway
How does the controller know it is falling?
Is there a specific part of the fall damage code* that is or isn't working?
corrected*
bool isFalling { get { return (controller.isGrounded && _directionY < 0); }}
This part is wrong:
&& _directionY < 0
_directionY is only ever a product of _jumpSpeed and multiplier
in the tutorial, how did they use what you have named _directionY ?
or rather, in place of it
Ah, I had to apply my versions of their variables to the script. They used a rigidbody to determine the velocity, however I don't have one. They did say that you could apply your own velocity script in that area, so I tried. I'll look for the proper one now.
Should be simple enough. Though it's 2.30 am here, so I'm not feeling the genius right now 😅
Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.
I'll try this! Thanks
private Vector3 velocity;
private Vector3 previous;
private void Update()
{
velocity = (transform.position - previous) / Time.deltaTime;
Debug.Log(velocity.y);
// other code
previous = transform.position; // must be at end of function
}
">" operator cannot be applied to operands of type "Vector3" ):
ah
sec, I must have misunderstood that code
I understand why
private float velocity;
private Vector3 previous;
private void Update()
{
velocity = (transform.position - previous).magnitude / Time.deltaTime;
// other code
previous = transform.position; // must be at end of function
}
This will, however, not allow you to differentiate between X and Y velocities - they are explicitly tied
Maybe it will be better 🤷♂️
Would I put previous or velocity into my isFalling bool?
Look at the Data Types of velocity and previous
and look at the solution in the code segment
Ah, my bad*
velocity is the thing you need for your isFalling check
in this code, you are using previous to calculate velocity
which is a position
Perhaps your character controller has a form of velocity
velocity = (transform.position - previous).y / Time.deltaTime;
that might work
It's getting too late for me to be any further helpful.
If you're still having issues tomorrow, I'll check by the thread. Good luck. o/