quake3-movement-unity3d icon indicating copy to clipboard operation
quake3-movement-unity3d copied to clipboard

Adding rocket jumping to this script

Open Pixelch3f opened this issue 7 years ago • 14 comments

Hi Zinglish,

Sorry if I'm spamming you as I've already attempted to message you on YouTube but I'm trying to get in contact with you for a little assistance!

I've been working with your script for a little while because I'm attempting to make an FPS game with movement similar to Quake.

However I've run into an issue, I'm trying to add rocket jumping to my game but because your script uses a character controller component it can't be affected by physics. I've attempted to code a knockback force function but it doesn't work exactly how I want it to. I've even tried to convert your script to use a Rigidbody instead of a Character Controller, but I can't get that working 100%.

Do you have any light to shed on the issue? I'm 100% happy to share the work I've done on your script.

Thanks!

Pixelch3f avatar Jan 12 '18 16:01 Pixelch3f

It likely won't work with a rigid body, mostly just because the movement script acts as its own isolated physics.

I've never used a rigid body in Unity, I've usually always written my own physics scripts so I am not sure how helpful I can be.

What happens when you call the knockback function? Does it just not do anything at all? Can you post the code? What about your implementation of a knockback function without rigidbody doesn't work for you?

WiggleWizard avatar Jan 12 '18 16:01 WiggleWizard

wow, quick reply wasn't expecting that ha! Thanks for that initially.

Here's the knockback code:

Player script: `public float knockbackSpeed = 4f; public float playerMass = 3f; public Vector3 knockbackImpact = Vector3.zero;

private void Update() { // This is to move the player if they are affected by a rocket explosion if (knockbackImpact.magnitude > 0.2f) playerCC.Move(knockbackImpact * Time.deltaTime); // moves the character between two vector3 locations per second knockbackImpact = Vector3.Lerp(knockbackImpact, Vector3.zero, knockbackSpeed * Time.deltaTime); } // function to apply force to the player when hit by a rocket explosion public void Knockback(Vector3 direction, float force) { direction.Normalize(); if (direction.y < 0) direction.y = -direction.y; // reflect down force on the ground knockbackImpact += direction.normalized * force / playerMass; }`

Rocket explosion script: `private Vector3 explosionOrigin; public float explosionRadius = 3f; public float explosionForce = 70f; public float explosionUpwardsForce = 1f;

private IEnumerator Start() { explosionOrigin = transform.position; // Wait one frame before starting, to ensure all objects are affected yield return null;

Collider[] objectsInRange = Physics.OverlapSphere(explosionOrigin, explosionRadius);

foreach (Collider collision in objectsInRange) { Player player = collision.GetComponent<Player>(); if (player != null) { // Apply knockback force to player if they are in explosion radius player.Knockback(player.transform.position - explosionOrigin, explosionForce); } }`

Sorry if the formatting for the code is rubbish I haven't really used GitHub before... is it readable?

I've also posted a question in Unity Answers where the code may be easier to read as well: https://answers.unity.com/questions/1453790/character-controller-rocket-jumping-adding-force-w.html

Oh and forgot to mention, the code does work, sort of, however not exactly how I want it to, because while the "force" is applied, I lose control of the character, I believe this is due to the Vector3.Lerp function

Pixelch3f avatar Jan 12 '18 17:01 Pixelch3f

I think I did it, pretty simple actually!

public void Knockback(Vector3 direction, float force) { direction.Normalize(); if (direction.y < 0) direction.y = -direction.y; // reflect down force on the ground playerVelocity += force * direction.normalized / playerMass; }

Used this instead and got rid of the code in Update. Code for explosion is the same. Works quite well, may need some tweaking though.

Pixelch3f avatar Jan 13 '18 15:01 Pixelch3f

Quick update, I've simplified the code and I'm happy with it now: public void Knockback(Vector3 direction, float force) { playerVelocity += force * direction / playerMass; playerController.Move(playerVelocity * Time.deltaTime); }

I put the .Move in there because it wasn't applying the force every single time. If you're interested in the explosion code let me know.

Pixelch3f avatar Jan 18 '18 11:01 Pixelch3f

Would be nice if you could contribute to this project for everyone :) could you make a pull request please??

WiggleWizard avatar Jan 18 '18 13:01 WiggleWizard

No problem, never done that before but I believe I have just done it! I also added a new file for the rocket explosion (I think) which is required for the knockback function to work, can you see that? Also I'm having another issue with air control I'm going to create a new issue thread.

Pixelch3f avatar Jan 18 '18 14:01 Pixelch3f

I don't see a pull request :P

Once you make a pull request I'll check it out, accept it and probably do some work to make it a drop in solution. I am sure many people are looking for something like this! :)

WiggleWizard avatar Jan 19 '18 08:01 WiggleWizard

Before checking these issues I wrote my own rocket jump addon, and it ended up very similar to what Pixelch3f put together. Now I am trying to get more advanced rocket jumping tricks to work, especially wall jumping with rockets. When I rocket jump at a wall, and then rocket jump off the wall, I expect to be propelled away from the wall, however, I often don't move. But I have also found that I can still climb the wall with a series of rocket jumps, and get speed boosts off of it if I want to do a horizontal jump away from it. Has anyone else run into this issue? I suspect that it may be the player's speed continuing in a direction toward the wall rather than getting launched away but I am new to unity and don't understand how to fix such an issue.

Power-Jake avatar Feb 02 '20 20:02 Power-Jake

You're pretty much correct: the velocity going into the wall continues even when you hit the wall and so the rocket you shoot attempts to add onto the velocity but is probably not enough to oppose the continued velocity. The script provided in this repo is simply just air control, not player-environment interaction. You'll have to handle that yourself.

WiggleWizard avatar Feb 03 '20 21:02 WiggleWizard

So I haven't implemented this myself, but since I have gotten busy and need to shelve my rocket jumping project for a while I thought I should share what I think would be a solution. I think that casting a ray to the point where the player collides with the object and then adding force in the opposite direction to cancel out the movement into the point of the collision might work. If I get time to try this solution myself I'll let y'all know how it goes.

Power-Jake avatar Feb 13 '20 02:02 Power-Jake

The way it's handled in Quake is to clip the player's velocity perpendicular to the surface hit. However it's a bit more complex than I described in that they resolve the clip with another clip until no more surfaces are hit. The method in question is here: https://github.com/id-Software/Quake-III-Arena/blob/master/code/game/bg_slidemove.c#L45 if you're interested in seeing the full implementation.

I assume you could achieve the same effect by querying the collision in Unity, then allowing Unity to resolve it for you.

WiggleWizard avatar Feb 13 '20 09:02 WiggleWizard

Cool! Thanks for the resource. I'll check that out when I have time.

Power-Jake avatar Feb 28 '20 21:02 Power-Jake

Hey guys! I know this is an old thread but I'm currently trying to make my own rocket jumping. I saw these scripts and decided to try it out, however I've been having trouble actually figuring out where all the scripts go and how this all works. If you wanna talk over discord it would be better for me since I don't check GitHub often. seth#4281

SethNerd avatar Jul 27 '20 22:07 SethNerd

Hey!

I'm sorry to bump this old repo, but for those, who might be wondering how to implement rocket jumping, I've found a code snippet shared by PhobicGunner on the unity forums. I'm not using Unity myself, but after migrating the code it all seems to work flawlessly so far (I'm working with GameStudio A8 + Lite-C language).

Here is original code snippet:

/// Add an explosion force to this character
/// </summary>
void AddExplosionForce( Vector3 explosionPos, float explosionRadius, float explosionForce )
{
    Vector3 explosionVec = transform.position - explosionPos; // vector from explosion to our center
    float distance = explosionVec.magnitude; // get distance to explosion
 
    if( distance > explosionRadius ) return; // we're outside the explosion's radius, ignore it
 
    float forceMult = 1f - ( distance / explosionRadius ); // 1.0 at explosion center, 0.0 at explosion radius
 
    explosionVec /= distance; // normalize the vector
    this.velocity += explosionVec * explosionForce * forceMult; // add explosion impulse to velocity
}

And here is the link to the forum thread: https://forum.unity.com/threads/wip-classic-fps-controller.317336/

Also big THANK YOU to WiggleWizard for sharing his work! It's awesome! Best regards!

3RUN avatar Jun 28 '21 15:06 3RUN