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
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;
}
Thank you @romatallinn I will give it a try and let you know!
@akpriest could you please accept the answer if it helped you? :)
@romatallinn I figured it out. Just had to change yLimit to yLimit.y and stop the force on the x position. Thanks