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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

[SCRIPTERS] Character "fly" system like gmod or minecraft. 1 Answer

Could anyone explain how to create an object(enemy in 2d game) in the Unity 5 moving from left to right and disappearing after crossing the screen? 1 Answer

How to make a character float up in a 2D game 0 Answers

calculate different angles in 2d 1 Answer

Starfox Zero-like airship movement help please :) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges