Question by 
               roblind3 · Dec 12, 2016 at 09:59 PM · 
                rotationscripting problemrigidbody2dvelocity  
              
 
              Inconsistent Velocity?
I'm not sure if this is the best place for this question, but any help would be appreciated. I am trying to create a simple 1 touch game where you touch to attach to a spinning wheel, and let go to be slung away.
I believe I have coding that works well enough, but the minor issue I'm having is with Velocity. When letting go of the wheel, I have it set perfectly to shoot off the correct direction, but the Velocity seems to be dependent on where you let go? I'm not sure why this is, but if anyone can take a look at my Script and give any feedback, I would greatly appreciate it. Thanks as always!
 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float speed = 360f;
     public float defaultGravity = 1.5f;
 
     //Parameters for Collider Detection
     public float objectCheckRadius = 2f;
     public LayerMask whatIsObject;
     public LayerMask whatIsGround;
 
     private bool touch = false; //Bool to determine if you're touching the screen
     private bool attached;      //Bool to determine if you're "attached" to the wheel
     private bool grounded = true;
 
     private Rigidbody2D body;
     private Transform obj;
 
     //Variables for Determining "Sling Shot" velocity
     private float slingShotSpeed;
     [Range(0, 1)]
     private float slingVelocityMult = 0.2f;
 
 
     // Use this for initialization
     void Start ()
     {
         body = GetComponent<Rigidbody2D>();
     }
 
     void Attach(Transform objAttached)
     {
         gameObject.layer = 10; //set layer to "Player" for Point Effector to start pulling object
         body.gravityScale = 0f;
 
         //rotate around object (obj) center at it's HingeJoint motor speed
         transform.RotateAround(objAttached.position, Vector3.back, objAttached.GetComponent<HingeJoint2D>().motor.motorSpeed * Time.deltaTime);
         
         //keep the local y axis pointed towards object center
         Vector3 dir = objAttached.transform.position - transform.position;
         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + 270;
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
     }
 
     //Make the player "Sling Shot" off the wheel after letting go
     void SlingShot()
     {
         attached = false;
         gameObject.layer = 0;
         body.gravityScale = defaultGravity;
         body.velocity = new Vector2(body.velocity.x * slingShotSpeed, body.velocity.y * slingShotSpeed);
     }
 
     void FixedUpdate()
     {
         grounded = Physics2D.OverlapCircle(transform.position, objectCheckRadius, whatIsGround);
 
         //When grounded, add "speed" force
         Vector3 movement = new Vector3(speed * Time.deltaTime, 0f, 0f);
         if (grounded)
         {
             body.AddForce(movement);
         }
 
         //If near a certain Collider, set it's transform as obj
         Collider2D[] objectColliders = Physics2D.OverlapCircleAll(transform.position, objectCheckRadius, whatIsObject);
         for (int i = 0; i < objectColliders.Length; i++)
         {
                 obj = objectColliders[i].transform;
         }
 
         //If not near a certain Collider, remove transform from obj
         if (objectColliders.Length == 0)
         {
             obj = null;
         }
     }
     
     void Update ()
     {
         
         if (obj)
         {
             //Bigger wheels need less force, Smaller wheels need more force
             switch (obj.ToString())
             {
                 case "Wheel Big (UnityEngine.Transform)":
                     slingVelocityMult = 0.05f;
                     break;
                 case "Wheel Small (UnityEngine.Transform)":
                     slingVelocityMult = 0.25f;
                     break;
             }
             slingShotSpeed = Mathf.Abs(obj.GetComponent<HingeJoint2D>().motor.motorSpeed * slingVelocityMult);
         }
 
         //Change "speed" to be positive or negative depending on it's current X Velocity
         switch (body.velocity.x < 0)
         {
             case true:
                 speed = -(Mathf.Abs(speed));
                 break;
             default:
                 speed = Mathf.Abs(speed);
                 break;
         }
 
         //Read if Screen is being touched
         if (Input.touchCount > 0)
         {
             if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 touch = true;
             }
             if (Input.GetTouch(0).phase == TouchPhase.Ended)
             {
                 touch = false;
             }
         }
 
         //Execute Attach function if touching a set object
         if (obj && touch)
         {
             attached = true;
             Attach(obj);
         }
 
         //Execute SlingShot function if attached but not touching a set object
         if(obj && attached && !touch)
         {
             SlingShot();
         }
     }
 }
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                