Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 cameroncronheimer · Jul 01, 2018 at 01:41 AM · rigidbodypositionmobilevelocityforce

Problem stopping rigid body after adding a force

In my game I have a balance board and a character that hops around on the board from being pushed with a relative force on its x-axis. I am trying to mimic Crossy Roads character movement, so I added a jump animation that parents my character object to change the y-axis by adding one to mimic a jump which works perfectly fine. The problem is I cannot use lerp the position because it doesn't work well moving my character (rigid body) on a balance board (rigid body). Therefore I have added a "if" statement that check when my character has reached its current x-position + 1 (local scale), when true I set my characters rigidbody velocity to zero to make it seem like you jump to a position on the board. When the board moves to fast my character seems to be floating, or when my character jumps off the board it floats in the air and slowly falls. I know the character floats in the air because I set the velocity to zero in my update and it only moves to a new position when I tap again. I have tried setting a timer on how long the velocity is set to 0, and I tried setting the x-velocity to zero and the y-velocity to -9.8 (gravity) but this seems to randomly jump in the air really high. SORRY for the rambling but all I need to set is the x- velocity to zero and keep the current state of the y-velocity to stop the movement on its local a-axis after a force has been applied and its "Jump To" position has been reached. Thank you m8s.

alt text alt text

 sing System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 
 public class Jumpy : MonoBehaviour 
     {
         
     float lastPosition;
     bool completion = true;
 
     Rigidbody rb;
 
 
     public Animator ani;
 
 
     public void Start() {
 
 
         rb = GetComponent<Rigidbody> ();
 
     }
 
 
     public void Update() {
 
 
 
         lastPosition = transform.position.x;
 
     //when moving right 
  //when hit position to stop velocity act as a jump to

         if (lastPosition >= (clickPosition + 0.85f)) {
 
 
             
         rb.velocity = new Vector3 (0, 0, 0);
 
 
             completion = true;
             ani.SetBool ("Jump", false);
 
 
 
 
         }
 
     }
 
     if(completion == true) {

    //when up adds a force to push rb to position
         if (Input.GetMouseButtonUp (0)) {
 
             

             completion = false;
             clickPosition = transform.position.x ;
     
         }
     }
 
     //just little animation things works fine if any one wants to copy feel free to make a cool jumping squish 
    animation
     if (Input.GetMouseButton (0)) {
 
         //ani.SetBool ("HoldDown", true);
 
     
         if (left == true) {
             if (transform.localScale.y >= 0.8f) {
                 transform.localScale -= transform.up * Time.deltaTime * 1.2f;
                 transform.localScale -= transform.forward * Time.deltaTime;
 
             }
         }
     } else {
 
         //ani.SetBool ("HoldDown", false);

         transform.localScale = Vector3.Lerp(transform.localScale,new Vector3(1.15f, 1.15f, 1.15f), Time.deltaTime * 10);
 
 
     }
 
     if (completion == false) {
         ani.SetBool("Jump",true);
 
         rb.AddRelativeForce (new Vector3 (1.5f, 0, 0), ForceMode.VelocityChange);
 
     }
 
     }
 }
 

unityforumq1.png (36.9 kB)
unityforumq2.png (21.6 kB)
Comment
Add comment · Show 2
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
avatar image CardboardComputers · Jul 01, 2018 at 05:23 AM 1
Share

I don't know if I'm understanding your conditions correctly, but can't you just set only the X velocity, like rb.velocity *= new Vector3 (0, 1, 1);, or rb.velocity = new Vector3 (0, rb.velocity.y, rb.velocity.z);?

avatar image cameroncronheimer CardboardComputers · Jul 02, 2018 at 09:43 PM 0
Share

Works great! thanks a-lot cheers!

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

163 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

Related Questions

How can you apply force or set the velocity of a rigidbidy on local axis? 1 Answer

Rigidbody Character 1 Answer

Is there a way to set velocity while setting the position? 1 Answer

How to get collision force, not relative velocity? 1 Answer

relativeVelocity how to? (noob question) 2 Answers


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