- Home /
2d Object floating in space HELP!
In my game you are an object floating in space and you move by clicking and dragging in the direction you want to go. Upon releasing your mouse you would go with whatever force in the direction you dragged. I'm currently working on modifying a script from an angry birds tutorial as i figured the motion was fairly similar and i may be able to make it work. I'm stuck at where i would write that i want the player to move in the direction of the mouse vs getting its velocity from the angry birds catapult. Sorry if I'm not making sense so i'm going to post three sets of code. The working angry birds code, my modified rework to try and weed out things i don't need, and the complete rewrite.
1 Angry Birds Code:
using UnityEngine;
using System.Collections;
public class PlayerMotion : MonoBehaviour
{
//Declare Public Variables:
public float maxStretch = 3.0f;
public LineRenderer catapultLineFront;
public LineRenderer catapultLineBack;
//Declare Private Variables:
private Transform catapult;
private SpringJoint2D spring;
private Ray rayToMouse;
private Ray playerToMouse; // distance between player and mouse
private Ray leftCatapultToProjectile;
private float maxStretchSqr;
private float circleRadius;
private bool clickedOn;
private bool isReleased;
private Vector2 prevVelocity;
private Vector2 maxVelocity;
void Awake ()
{
spring = GetComponent <SpringJoint2D> ();
catapult = spring.connectedBody.transform;
}
void Start ()
{
LineRendererSetup ();
rayToMouse = new Ray (catapult.position, Vector3.zero);
leftCatapultToProjectile = new Ray (catapultLineFront.transform.position, Vector3.zero);
maxStretchSqr = maxStretch * maxStretch;
CircleCollider2D circle = collider2D as CircleCollider2D;
circleRadius = circle.radius;
}
void Update ()
{
if (clickedOn)
Dragging ();
//checkRelease (); //Method to see when mouse is released to give velocity in that direction based on object to mouse positioning.
//if (ReleaseObject)
//Launched ();
if (spring != null) { //probably not needed for what i'm trying to do
if (!rigidbody2D.isKinematic && prevVelocity.sqrMagnitude > rigidbody2D.velocity.sqrMagnitude) {
Destroy (spring); //not needed
rigidbody2D.velocity = prevVelocity;
}
if (!clickedOn)
//maxVelocity = //DISTANCE FROM MOUSE TO OBJECT * something....
prevVelocity = rigidbody2D.velocity;
LineRendererUpdate (); //Probably not needed
} else {// the following should not be needed
catapultLineFront.enabled = false;
catapultLineBack.enabled = false;
}
}
/* NOT DONE WITH THIS METHOD!!!
void checkRelease ()
{
Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 playerToMouse = mouseWorldPoint - player.position;
if (catapultToMouse.sqrMagnitude > maxStretchSqr) {
rayToMouse.direction = catapultToMouse;
mouseWorldPoint = rayToMouse.GetPoint (maxStretch);
}
//mouseWorldPoint.z = 0f;
//transform.position = mouseWorldPoint;
}
*/
//LineRenderer (rubber band) should not be needed
void LineRendererSetup ()
{
catapultLineFront.SetPosition (0, catapultLineFront.transform.position);
catapultLineBack.SetPosition (0, catapultLineBack.transform.position);
catapultLineFront.sortingLayerName = "Foreground";
catapultLineBack.sortingLayerName = "Foreground";
catapultLineFront.sortingOrder = 3;
catapultLineBack.sortingOrder = 1;
}
void OnMouseDown ()
{
spring.enabled = false;
clickedOn = true;
//isReleased = false;
rigidbody2D.isKinematic = true;
}
void OnMouseUp ()
{
spring.enabled = true;
rigidbody2D.isKinematic = false;
clickedOn = false;
//isReleased = true;
//
}
void Dragging ()
{
Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
if (catapultToMouse.sqrMagnitude > maxStretchSqr) {
rayToMouse.direction = catapultToMouse;
mouseWorldPoint = rayToMouse.GetPoint (maxStretch);
}
mouseWorldPoint.z = 0f;
transform.position = mouseWorldPoint;
}
void LineRendererUpdate ()
{
Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position;
leftCatapultToProjectile.direction = catapultToProjectile;
Vector3 holdPoint = leftCatapultToProjectile.GetPoint (catapultToProjectile.magnitude + circleRadius);
catapultLineFront.SetPosition (1, holdPoint);
catapultLineBack.SetPosition (1, holdPoint);
}
/*void Launched () {
Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
if (catapultToMouse.sqrMagnitude > maxStretch) {
rayToMouse
mouseWorldPoint.z = 0f;
//assigns position to mouse
//transform.position = mouseWorldPoint;
}*/
}
2 'slightly' modified version:
using UnityEngine;
using System.Collections;
public class PlayerMotion : MonoBehaviour
{
//Declare Public Variables:
public float maxStretch = 3.0f;
public float velocityVar = 5.0f; //setable velocity
public LineRenderer catapultLineFront;
public LineRenderer catapultLineBack;
//Declare Private Variables:
private Transform catapult;
private Transform player;
private SpringJoint2D spring;
private Ray rayToMouse;
private Ray playerToMouse; // distance between player and mouse
private Ray leftCatapultToProjectile;
private float maxStretchSqr;
private float circleRadius;
private bool clickedOn;
private bool releasedObject;
private Vector2 prevVelocity;
private Vector2 maxVelocity;
/*void Awake ()
{
//spring = GetComponent <SpringJoint2D> ();
//catapult = spring.connectedBody.transform;
}*/
void Start ()
{
//LineRendererSetup ();
//leftCatapultToProjectile = new Ray (catapultLineFront.transform.position, Vector3.zero);
//maxStretchSqr = maxStretch * maxStretch;
CircleCollider2D circle = collider2D as CircleCollider2D;
circleRadius = circle.radius;
}
void Update ()
{
/*if (clickedOn)
Dragging ();
//checkRelease (); //Method to see when mouse is released to give velocity in that direction based on object to mouse positioning.
*/
player = rigidbody2D.transform; //assign coord's for player
rayToMouse = new Ray (player.position, Vector3.zero);
if (releasedObject)
Launched ();
if (!releasedObject)
maxVelocity = velocityVar;
/*if (spring != null) { //probably not needed for what i'm trying to do
if (!rigidbody2D.isKinematic && prevVelocity.sqrMagnitude > rigidbody2D.velocity.sqrMagnitude) {
Destroy (spring); //not needed
rigidbody2D.velocity = prevVelocity;
}*/
if (!clickedOn)
//maxVelocity = //DISTANCE FROM MOUSE TO OBJECT * something....
prevVelocity = rigidbody2D.velocity;
//LineRendererUpdate (); //Probably not needed
} /*else {// the following should not be needed
catapultLineFront.enabled = false;
catapultLineBack.enabled = false;
}*/
}
void releasedObject ()
{
Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 playerToMouse = mouseWorldPoint - player.position;
if (catapultToMouse.sqrMagnitude > maxStretchSqr) {
rayToMouse.direction = catapultToMouse;
mouseWorldPoint = rayToMouse.GetPoint (maxStretch);
}
//mouseWorldPoint.z = 0f;
//transform.position = mouseWorldPoint;
}
/*/LineRenderer (rubber band) should not be needed
void LineRendererSetup ()
{
catapultLineFront.SetPosition (0, catapultLineFront.transform.position);
catapultLineBack.SetPosition (0, catapultLineBack.transform.position);
catapultLineFront.sortingLayerName = "Foreground";
catapultLineBack.sortingLayerName = "Foreground";
catapultLineFront.sortingOrder = 3;
catapultLineBack.sortingOrder = 1;
}*/
void OnMouseDown ()
{
//spring.enabled = false;
//clickedOn = true;
releasedObject = false;
//rigidbody2D.isKinematic = true;
}
void OnMouseUp ()
{
//spring.enabled = true;
//rigidbody2D.isKinematic = false;
//clickedOn = false;
releasedObject = true;
//
}
/*void Dragging ()
{
Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
if (catapultToMouse.sqrMagnitude > maxStretchSqr) {
rayToMouse.direction = catapultToMouse;
mouseWorldPoint = rayToMouse.GetPoint (maxStretch);
}
mouseWorldPoint.z = 0f;
transform.position = mouseWorldPoint;
}*/
/*void LineRendererUpdate ()
{
Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position;
leftCatapultToProjectile.direction = catapultToProjectile;
Vector3 holdPoint = leftCatapultToProjectile.GetPoint (catapultToProjectile.magnitude + circleRadius);
catapultLineFront.SetPosition (1, holdPoint);
catapultLineBack.SetPosition (1, holdPoint);
}*/
/*void Launched () {
Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
if (catapultToMouse.sqrMagnitude > maxStretch) {
rayToMouse
mouseWorldPoint.z = 0f;
//assigns position to mouse
//transform.position = mouseWorldPoint;
}*/
}
3 Complete Rewrite:
using UnityEngine;
using System.Collections;
public class playerMovement : MonoBehaviour {
public float velocityVar = 5.0f; //customizable velocity
private Transform player;
private Ray rayToMouse;
private Ray playerToMouse;
private float circleRadius;
private bool clickedOn;
private bool isReleased;
private Vector2 maxVelocity;
private Vector2 prevVelocity;
void Start () {
CircleCollider2D circle =collider2D as CircleCollider2D;
circleRadius = circle.radius;
}
void Update () {
player = rigidbody2D.transform; //player coords
rayToMouse = new Ray (player.position, Vector3.zero);
if (isReleased)
rigidbody2D.velocity = velocityVar
}
Answer by 19Logan91 · Jul 11, 2014 at 10:17 AM
OOOOO kay. So while no one responding I figured someone may run into this in the future. So for the sake of not wasting virtual space here is the answer of which I was seeking.
void Update () {
player = rigidbody2D.transform; //player coords
rayToMouse = new Ray (player.position, Vector3.zero);
if (isReleased)
rigidbody2D.velocity = velocityVar
}
This is the code where I was running into issue. Pretty much i just needed to write something to solve for distance between mouse and object. An on mouse up method has been added(see below) that gives the options of toward or away from mouse.
public float velocityVar = 10;
private SnapDir snapDirection = SnapDir.toward;
private enum SnapDir {toward, away}
private Vector2 forceMultiplier;
void Update () {
player = rigidbody2D.transform;
Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
forceMultiplier = mouseWorldPoint - player.position;
}
void OnMouseUp () {
if (snapDirection == SnapDir.away)
velocityVar = velocityVar * -1;
rigidbody2D.AddForce (velocityVar * forceMultiplier * rigidbody2D.mass);
}
So there we go. Drop me questions on my twitter @blueshoesyes or here. The idea of this code was taken from the following *Link*
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
(C#) Public variables that have descriptions in the engine. 2 Answers
How do i make my person move forward in the direction of the cursor 2 Answers
Distribute terrain in zones 3 Answers
Can anyone fix this State.cs Code? 0 Answers