Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alemalespin97 · Aug 12, 2021 at 12:52 AM · physicsrigidbodyaddforceplayer movementlaunch

How to apply a force after launching the player?

I implemented a mechanic in my game that launches the player with the right mouse button in the direction of the first person camera. It is working as intended, but when the player launches into the air, they come to an abrupt stop and come straight back down once a certain travel distance is achieved. So at the end of the launch, I want to apply a force to the player in the same direction so that there is a bit of an arc to the fall. I've tried using addForce on a Rigidbody attached to the player, and a Boolean to trigger it at the end of the launch, but no luck. Any ideas on how to implement this?

 public class PlayerMov : MonoBehaviour
 {
     enum MovementMode
     {
         Platformer,
         Strafe
     }
 
     [SerializeField] //makes variable visible in inspector, despite it being private
     private MovementMode _movementMode = MovementMode.Strafe;
     [SerializeField]
     private float _walkSpeed = 3f;
     [SerializeField]
     private float _runningSpeed = 6f;
     [SerializeField]
     private float _gravity = 9.81f;
     [SerializeField]
     private float _gravityPlatformer = -12f;
     [SerializeField]
     private float _jumpSpeed = 3.5f;
     [SerializeField]
     private float _doubleJumpMultiplier = 0.5f;
     [SerializeField]
     private GameObject _cameraRig;
 
     public float jumpHeight = 1;
 
     private CharacterController _controller;
 
     private float _directionY;
     private float _currentSpeed;
 
     private bool _canDoubleJump = false;
 
     public float turnSmoothTime = 0.2f;
     float turnSmoothVelocity;
 
     public float speedSmoothTime = 0.1f;
     float speedSmoothVelocity;
 
     private float velocityY;
 
     //for game over screen
     PlayerInfo playerInfo;
 
     //to prevent gravity increasing forever
     public float terminalVelocity = -3;
 
     //for Bounce
     public Transform camera;
     public float bounceSpeed = 5f;
     public float bounceDistance = 10f;
     public float forceMultiplier = 1; //*************************************
     private bool bounceActive;
     private bool bouncePlayer;
     private bool loaded;
     public bool freeze;
     public bool bounceEnded; //******************************************
     SoldierMov soldier;
     Vector3 direction;
 
 
     //for tracking distance when bouncing
     private Vector3 oldPos;
     private float totalDistance = 0;
 
     //Bounce sound effects
     public AudioSource bounceLoadedInitial;
     public AudioSource bounceLoadedLoop;
     public AudioSource bounceLaunch;
 
     void Start()
     {
         _controller = GetComponent<CharacterController>();
         playerInfo = GameObject.Find("Player").GetComponent<PlayerInfo>();
         soldier = GameObject.Find("Soldier_Prefab").GetComponent<SoldierMov>();
 
         //variables for bounce
         bounceActive = false;
         bouncePlayer = false;
         loaded = false;
         freeze = false;
         bounceEnded = false; //************************************************
     }
 
     void Update()
     {
         //Debug.Log("alive = " + playerInfo.GetAlive());
 
         if (playerInfo.GetAlive() && freeze == false) //disables player movement if dead or freezing time with bounce
         {
             if (_movementMode == MovementMode.Strafe)
             {
                 MovementStafe();
             }
 
             if (_movementMode == MovementMode.Platformer)
             {
                 MovementPlatformer();
             }
         }
 
         if (playerInfo.GetAlive()) //disables bounce if dead
         {
             //loads Bounce with right mouse button when near soldier, and launchs player upon button release
             soldier = GameObject.Find("Soldier_Prefab(Clone)").GetComponent<SoldierMov>();
             if (Input.GetButtonDown("Fire2") && soldier.playerNear) //load bounce when near soldier
             {
                 //Debug.Log("BOUNCE IS LOADED");
 
                 oldPos = transform.position; //save position when bounce is loaded
                 loaded = true; //loads the bounce
                 freeze = true; //used to freeze player and soldier
 
                 //Play loaded bounce sound effect
                 bounceLoadedInitial.Stop();
                 bounceLoadedInitial.Play(); //play initial sfx
                 bounceLoadedLoop.PlayDelayed(bounceLoadedInitial.clip.length); //loop the second part of the sfx
             }
             else if (Input.GetButtonUp("Fire2") && loaded) //execute bounce upon button release
             {
                 bounceLoadedLoop.Stop();
                 bounceLaunch.Play(); //play bounce launch sfx
                 direction = new Vector3(camera.forward.x, camera.forward.y, camera.forward.z);
                 bouncePlayer = true;
             }
             //Debug.Log("freeze = " + freeze + "\nloaded = " + loaded);
 
             if (bouncePlayer)
             {
                 Bounce();
             }
             else
             {
                 bounceEnded = false;
             }
 
             if (bounceEnded)
             {
                 //use addforce on rigidbody
                 Rigidbody rb = GetComponent<Rigidbody>();
                 rb.AddForce(direction * forceMultiplier, ForceMode.Impulse);
             }
             Debug.Log("bounceEnded = " + bounceEnded);
         }
     }
 
     private void LateUpdate()
     {
         if (IsPlayerMoving() && _movementMode == MovementMode.Strafe)
             transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, _cameraRig.transform.localEulerAngles.y, transform.localEulerAngles.z);
     }
 
     private bool IsPlayerMoving()
     {
         return Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0;
     }
 
     private void MovementStafe()
     {
         float horizontalInput = Input.GetAxis("Horizontal");
         float verticalInput = Input.GetAxis("Vertical");
 
         Vector3 moveDirection = new Vector3(horizontalInput, 0, verticalInput);
 
         if (_controller.isGrounded)
         {
             _canDoubleJump = true;
 
             if (Input.GetButtonDown("Jump"))
             {
                 _directionY = _jumpSpeed;
             }
         }
         else
         {
             if (Input.GetButtonDown("Jump") && _canDoubleJump)
             {
                 _directionY = _jumpSpeed * _doubleJumpMultiplier;
                 _canDoubleJump = false;
             }
         }
 
         if (bounceActive == false) //gravity is active when bounce is inactive
         {
             //prevents gravity from increasing forever
             if (_directionY > terminalVelocity)
             {
                 _directionY -= _gravity * Time.deltaTime;
             }
             else
             {
                 _directionY = terminalVelocity;
             }
         }
         else //turn off gravity when bounce is active
         {
             _directionY = 0;
         }
         //Debug.Log("_directionY = " + _directionY);
 
         moveDirection = transform.TransformDirection(moveDirection);
 
         bool running = Input.GetKey(KeyCode.LeftShift);
         float targetSpeed = (running) ? _runningSpeed : _walkSpeed;
         _currentSpeed = Mathf.SmoothDamp(_currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
 
         moveDirection.y = _directionY;
 
         _controller.Move(_currentSpeed * Time.deltaTime * moveDirection);
     }
 
     private void MovementPlatformer()
     {
         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
         Vector2 inputDir = input.normalized;
         bool running = Input.GetKey(KeyCode.LeftShift);
 
         if (inputDir != Vector2.zero)
         {
             float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + _cameraRig.transform.eulerAngles.y;
             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
         }
 
         float targetSpeed = ((running) ? _runningSpeed : _walkSpeed) * inputDir.magnitude;
         _currentSpeed = Mathf.SmoothDamp(_currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
 
         velocityY += Time.deltaTime * _gravityPlatformer;
         Vector3 velocity = transform.forward * _currentSpeed + Vector3.up * velocityY;
 
         _controller.Move(velocity * Time.deltaTime);
         _currentSpeed = new Vector2(_controller.velocity.x, _controller.velocity.z).magnitude;
 
         if (_controller.isGrounded)
         {
             velocityY = 0;
         }
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             if (_controller.isGrounded)
             {
                 float jumpVelocity = Mathf.Sqrt(-2 * _gravityPlatformer * jumpHeight);
                 velocityY = jumpVelocity;
             }
         }
     }
 
     private void Bounce()
     {
         //track distance traveled during bounce
         Vector3 distanceVector = transform.position - oldPos;
         float distanceThisFrame = distanceVector.magnitude;
         totalDistance += distanceThisFrame;
         oldPos = transform.position;
 
         //makes player travel a certain distance
         if (totalDistance < bounceDistance) //launch player until bounce distance is achieved
         {
             bounceActive = true; //turns off gravity
             _controller.Move(direction * bounceSpeed * Time.deltaTime);
         }
         else //stop Bounce once bounceDistance is achieved
         {
             //use addforce on rigidbody
             //Rigidbody rb = GetComponent<Rigidbody>();
             //rb.AddForce(direction * forceMultiplier, ForceMode.Impulse);
 
             bounceActive = false; //turn gravity back on
             bouncePlayer = false; //used to stop calling this function
             loaded = false;
             freeze = false;
             bounceEnded = true; //***************************************************************
             totalDistance = 0;
         }
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

236 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody cube randomly stops moving? 1 Answer

Why AddForce up is higher than AddForce right? 0 Answers

transform.forward not always forward? 2 Answers

Why wont my character jump? (using rigidbody and addForce) 1 Answer

making perpetual movement 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges