Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 19Logan91 · Jul 10, 2014 at 09:37 AM · c#space

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
     }
 
 
 
 

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
Best Answer

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*

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

2 People are following this question.

avatar image avatar image

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


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