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 StigC · May 11, 2017 at 06:40 PM · movementrigidbody2dgravitymovetowardsjumping object

Make object jump to fixed y position

Hi there,

I'm trying to replicate a game for good'ol'practice's sake. It's getting there, but I've had issues with my player/ball movement ever since I started the project.

Here's a little video showing my problem: YOUTUBE LINK

Problem is my ball won't jump a fixed height. In fact, it seems to change the more I move my mouse around. I guess moving in the x axis has an effect on the speed?


Current settings for the players Rigidbody2D: Rigidbody2D settings


Current Physics2D settings: Physics2D settings


And finally, the script attached to the player:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BallControl : MonoBehaviour {
 
     private bool hasStarted = false;
     public GameObject smoke;
     public bool hitSomething = false;
     private Vector2 bounceTweak;
 
 
 
 
     void Start ()
     {
         
     }
     
 
     void FixedUpdate ()
     {
         MoveWithMouse();
 
 
 
         if(!hasStarted)
         {
             this.GetComponent<Rigidbody2D> ().gravityScale = 0;
 
             //Wait for a mousepress to launch.
             if(Input.GetMouseButtonDown(0))
             {
                 hasStarted = true;
                 print("Mouse Clicked! Game has started!");
                 this.GetComponent<Rigidbody2D>().velocity = new Vector2 (0f, 10f);
                 this.GetComponent<Rigidbody2D> ().gravityScale = 1;
 
 
             }
         }
 
     }
 
 
     void MoveWithMouse()
     {
         Vector3 ballPos = new Vector3(0f, transform.position.y, 0f);
         float mousePosInBlocks = Input.mousePosition.x / Screen.width * 6;
         ballPos.x = Mathf.Clamp(mousePosInBlocks, 0f,6f);
 
         this.transform.position = ballPos;
     }
 
 
     public void OnCollisionEnter2D(Collision2D col)
     {
         hitSomething = true;
         if (hitSomething == true)
         {
             print ("WE HIT SOMETHING");
             //Vector2 bounceTweak = new Vector2 (Random.Range(0f, 10f), Random.Range(0f, 10f));
             //Vector2 tweak = new Vector2 (Random.Range(0f, 0.5f), Random.Range(0f,0.2f));
             hitSomething = false;
         }
 
 
 
         if (hasStarted == true)
         {
             SmokeEffect();
             this.GetComponent<AudioSource>().Play();
             //this.GetComponent<Rigidbody2D>().velocity += bounceTweak;
             //this.GetComponent<Rigidbody2D>().velocity += tweak;
         }
     }
 
 
     void SmokeEffect()
     {
         GameObject smokePuff = Instantiate(smoke, this.transform.position, Quaternion.identity);
         smokePuff.GetComponent<ParticleSystem>().startColor = this.GetComponent<SpriteRenderer>().color;
     }
 }
 



I've googled around and bumped into a MoveTowards function. Could that work? If so, how do I limit MoveTowards to just affect one axis?

Cheers!

screen-shot-2017-05-11-at-202100.png (188.2 kB)
screen-shot-2017-05-11-at-202043-1.png (166.2 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Matthewj866 · May 11, 2017 at 10:42 PM

Hi there.

I am unsure as to what is causing the problem, however the way I tend to get things to fixed distances using physics is good old speed = distance / time.

So, when the ball collides with one of the blocks at the bottom, you can simply have it adjust the velocity in FixedUpdate() so when the collision occurs you set some boolean to true to send it upwards a fixed distance. You could then recalculate this every frame to make it gradually slow down until it hits that specific point on the Y axis.

Example:

 void FixedUpdate()
 {
     if(isTravellingUp)
     {
         if(transform.position.y >= vectorForLimit.y)
         {
             isTravellingUp = false;
         }
         else
         {
             _rigidBody.velocity = (vectorForLimit.y - transform.position.y) / yourTimeValue;
         }
     }
 }

You can then just use the transform to make the ball follow left or right as you were originally doing.

Hope this helps!

Comment
Add comment · Share
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

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

90 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

Related Questions

Can someone help me with this Rigidbody2D bug? 1 Answer

Why does this piece of code work with gravity and this one dosent 2 Answers

toss up game object into air with freefall physics 1 Answer

How do I get my character to keep his left and right momentum in the air while also disabling movement controls until grounded? 0 Answers

Player slides down slope. 0 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