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

0 Replies

· Add your reply
  • Sort: 

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

110 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 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

How do I make a 2D spaceship rotate to my mouse? 0 Answers

Rotate Rigidbody2D by Touch 1 Answer

Why is a gameobject moves at start? 0 Answers

Make Prefab Rotate Depending on a different Game Object's Rotation. 0 Answers

Orbit object around parent object 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