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 /
  • Help Room /
avatar image
0
Question by akpriest · Oct 28, 2016 at 09:06 PM · 2d gamephysics2d

Launching character up and stopping on same horizontal position

Hello everyone.

I made a game where the player has the ability to click on the enemy character and drag them any direction. When the player lets go, I calculate the direction and a force and apply it to the enemy, thus launching them in that direction. Gravity is then added to the rigidbody of the enemy causing the enemy to go up and then eventually come back down. The problem I am having is that I want the enemy to stop at the same horizontal axis where he was when he was first clicked on. I am having some issues getting the player to stop where he first left. Does any one have any ideas how I can do this?

I have attached my code and a picture explaining below. If their is any way i can attach video or get video to you please let me know!

 using UnityEngine;
 using System.Collections;
 
 public class UserInput : MonoBehaviour {
 
     public EnemyMovment enemyMovment;
     const float LAUNCH_SPEED = 3000;
     //store gameObject reference
     public Object BottomWall;
     GameObject enemyBottomWall;
     public Transform castle;
     public GameObject Enemy_Obj;
     public Rigidbody2D rigidbody;
     public float movesSpeed = 10;
     //public Camera camera;
 
     //holds mouse position
     Vector3 mouseLastPosition;
     Vector3 mouseCurrentPosition;
     Vector3 mouseDiffrenceInPosition;
     Vector3 skeletonXPosition;
 
     int counter;
     Vector2 mMouseDownPos;
     Vector2 mMouseUpPos;
     public float speed;
     public float throwLeftOrRight;  // positive throws right and left throws blank
     public float throwUpOrDown;     // positive throws up and negative throws down
     public Transform skeleton;              //castle target
     public SpriteRenderer spriteRenderer; //sprite render refrence
     private Animator animator;            //animator refrence
     public Sprite Idle;
 
     // Use this for initialization
     void Start () 
     { 
 
         //gets animator for animator refrence
         animator = GetComponent<Animator>();
         rigidbody = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update () 
     {
 
     
 
         //statment that will save old position every other time to increase distances for projection calculations
         if (counter % 2 == 0) 
         {
             //Saves last mouse poition
             mouseLastPosition = mouseCurrentPosition;
         }
         //Gets new mouse position
         mouseCurrentPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10);
 
         //increments counter for if statment
         counter++;
 
         //function that checks when the mouse button is released. It will performe the actions below when the mouse is released
         if (Input.GetMouseButtonUp (0))
         {
 
             //Ingnores collision between wall layer and building layer
             //Allows enemy to pass threw buildings when picked up
             Physics2D.IgnoreLayerCollision(8, 9,true);
         
             //Calculates the diffrence in position between last mouse position and current
             mouseDiffrenceInPosition = mouseCurrentPosition - mouseLastPosition;
 
             //calculates which way to throw character based on mouse position diffrence * launch speed
             throwLeftOrRight = (mouseDiffrenceInPosition.x / 2)* LAUNCH_SPEED;
             throwUpOrDown = (mouseDiffrenceInPosition.y / 2) * LAUNCH_SPEED;
 
             //Throws enemy in direction calculated by mouse position
             rigidbody.AddForce(new Vector2(throwLeftOrRight, throwUpOrDown));
 
             //adds gravity to enemy
             rigidbody.gravityScale = 16;
         }
 
 
     }
 
     //function that detects if the enemy has the mouse over them
     public void OnMouseOver(){
 
         //If statment that detects if the enemy has been clicked on
         if (Input.GetMouseButtonDown(0)) {
 
 
             Enemy_Obj.layer = 8;               //sets enemy layer to wall layer
             spriteRenderer.sprite = Idle;      //changes sprite to idle sprite
             animator.Stop();                   //stops animation
             enemyMovment.stopWalking = true;   //makes player stop walking
 
             //Gets the x position of the enemy
             skeletonXPosition = transform.position;
             //sets the y and z to zero
             skeletonXPosition.y = 0;
             skeletonXPosition.z = 0;
 
         }
     }
 
     //drags the enemy with the mouse
     public void OnMouseDrag()
     {
         //moves skeleton to mouse position
         Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10);
         Vector3 objectPosition = Camera.main.ScreenToWorldPoint (mousePosition);
 
         transform.position = objectPosition;
 
     }
 }

[1]: /storage/temp/81119-unity-example.png

unity-example.png (63.5 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
1
Best Answer

Answer by romatallinn · Oct 28, 2016 at 10:18 PM

If the player's Y-coordinate is lower than the lowest possible Y (for example, when the user started to drag the player, just save the player's Y coordinate somewhere), then return him back to the lowest Y position, and you should turn off the gravity at this moment.

In other words,

 if(transform.position.y < yLimit){
 
      transform.position = new Vector2(transform.position.x, yLimit);
      rigidbody.useGravity = false;
 
 }
Comment
Add comment · Show 4 · 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
avatar image akpriest · Oct 29, 2016 at 07:06 PM 0
Share

Thank you @romatallinn I will give it a try and let you know!

avatar image romatallinn akpriest · Nov 10, 2016 at 10:31 PM 0
Share

@akpriest could you please accept the answer if it helped you? :)

avatar image akpriest romatallinn · Nov 18, 2016 at 07:44 PM 0
Share

@romatallinn I figured it out. Just had to change yLimit to yLimit.y and stop the force on the x position. Thanks

Show more comments

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

82 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

Related Questions

Looking for a proper way to solve jumping collision issue in fighting game. 0 Answers

Someone help me with Physics2D material Bounce? 0 Answers

Do I need Rigid2DBody if I don't plan to use physics? 2 Answers

stick-figure piercing 0 Answers

Raycast2D from camera does not hit objects 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