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 /
  • Help Room /
avatar image
0
Question by unity_N2KDSek8n06ERA · Jul 14, 2020 at 07:02 AM · flying

Flying around an edge of a Cube

So the player drags and releases object (red circle) like in angry birds. On all 4 sides of the cube the object follows the flat surface of the cube (imagine top and bottom of the cube are infinitely long). When the red circle reaches an edge it should fly within a curve (red line) to the next side of the cube based on its direction and velocity. To put it simple it is like in Mario Odyssey where Pokio (The bird with the long spike) can fly around edges.

My problem is that I do not know how to get the red circle from one side to the other. I used raycast, gravity and bezier curves but nothing worked out for me.

alt text

test.png (9.9 kB)
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

Answer by unity_N2KDSek8n06ERA · Jul 14, 2020 at 07:32 AM

 public class PlayerMovement : MonoBehaviour
 {
     public CameraMovement camMove;
 
     public float launchSpeed;
 
     [SerializeField]
     private float treshold = 0.35f;
     [SerializeField]
     private float adjusting = -3.0f;
     [SerializeField]
     private float maxDragDelta = 2.0f;
     [SerializeField]
     private float shaking = 1.0f;
 
     private Rigidbody rb;  
     private Vector3 hook;
     private Vector3 direction;
     private Vector3 force;
     private Animator idle;
 
     private float distance;
     private bool started;
     private float calculatedDistance;
 
     private enum State { PAUSE, DRAGGING, FLYING }
     private static State playerState;
 
     void Awake()
     {
         //idle = GetComponent<Animator>();
 
         playerState = State.PAUSE;
         //IdleAnimation();
       
         rb = GetComponent<Rigidbody>();
         started = true;
         calculatedDistance = adjusting - Camera.main.transform.position.z;
     }
 
     void Update()
     {
         /*if(Input.touchCount == 1)
         {      
             switch (Input.GetTouch(0).phase)
             {
                 case TouchPhase.Began:
                     InitializePosition();
                     break;
                 case TouchPhase.Moved:
                     Vector3 touchPos = Input.GetTouch(0).position;
                     SetPosition(touchPos);
                     break;
                 case TouchPhase.Ended:
                     CheckDistance();
                     //camMove.SwitchStates(IsDraging());
                     break;
             }
         }*/
       
         if (hasStarted())
         {
             Vector3 mousePos = Input.mousePosition; 
             if (Input.GetMouseButtonDown(0))
             {
                 InitializePosition();
             }
             else if (Input.GetMouseButton(0))
             {                       
                 SetPosition(mousePos);
             }
             else if(Input.GetMouseButtonUp(0))
             {
                 if (playerState.Equals(State.DRAGGING))
                 {
                     CheckDistance();
                      //camMove.SwitchStates(IsDraging());
                 }               
             }
         }
 
 
         /* rotate stack based on drag angle and player speed */
         /*if (playerState.Equals(State.FLYING))
         {
             Flying();
         }*/
     }
 
     private void InitializePosition()
     {
         playerState = State.PAUSE;
         //IdleAnimation();
 
         rb.velocity = Vector3.zero;
         rb.angularVelocity = Vector3.zero;
 
         rb.useGravity = false;
         hook = transform.position;
 
         if (!playerState.Equals(State.FLYING))
         {
             playerState = State.DRAGGING;
             //IdleAnimation();
              
             //camMove.SwitchStates(IsDraging());          
         }
         else
         {
             playerState = State.PAUSE;
         }
     }
 
     private void SetPosition(Vector3 playerPos)
     {
         playerPos.z = calculatedDistance;
         Vector3 newVector = Camera.main.ScreenToWorldPoint(playerPos);
         newVector.z = adjusting;
         //transform.LookAt(hook);
 
         distance = Vector3.Distance(newVector, hook);
         direction = (newVector - hook).normalized;
 
         if(distance >= maxDragDelta)
         {            
             rb.position = hook + direction * maxDragDelta;
             distance = maxDragDelta;
 
             //shake player
             Vector3 newPos = Random.insideUnitSphere * (Time.deltaTime * shaking);
             newPos.x = rb.position.x + newPos.x * direction.x;
             newPos.y = rb.position.y + newPos.y * direction.y;
             newPos.z = rb.position.z;            
             rb.position = newPos;
         }
         else
         {        
             rb.position = newVector; 
         }     
     }
 
     private void CheckDistance()
     {
         transform.eulerAngles = Vector3.zero;
         if (distance >= treshold)
         {
             playerState = State.FLYING;
 
             rb.useGravity = true;
             force = direction * distance * launchSpeed;
             rb.AddForce(-force, ForceMode.Impulse);           
         }
         else
         {
             playerState = State.PAUSE;
             //IdleAnimation();
             
             rb.position = hook;         
         }
     }
 
     private void Flying()
     {
         
     }
 
     private void IdleAnimation()
     {
         if (playerState.Equals(State.PAUSE))
         {
             idle.SetBool("idle", true);
         }
         else
         {
             idle.SetBool("idle", false);
         }        
     }
 
     public Vector3 getDirection()
     {
         return direction;
     }
 
     public bool hasStarted()
     {
         return started;
     }
 }
 
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

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

Unexpected symbol '{' error in c# if statement 2 Answers

Make an energy wich decreases when the character jumps 2 Answers

How to make the Character fly 0 Answers

how to make a running player fly through the air at a particular distance from the ground 0 Answers

3D Floating Enemy Pathfinding 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