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 MuradD · Feb 29, 2016 at 12:22 PM · 2d2d game2d-gameplay2d rotation

clamp position 1/4 of a circle 2D

I want to Make script where 2D object moves in 1/4th of a circle, Quarter 1 of a circle. with maximum distance is 4.0f for example.

what I could do 2D java script not much.

 private var pos : Vector2;
 var cannons : Transform;
 var maxDistance : float = 4.0f;
 
     function Start () {
     
     }
     
     function Update () {
         var dist = Vector2.Distance(cannons.position, transform.position);
         if(maxDistance < dist){
             dist = maxDistance;
         }
         print ("Distance to other: " + dist);
         if (Input.touchCount > 0){
             pos = Camera.main.ScreenToWorldPoint (Vector2 (Input.GetTouch(0).position.x, Input.GetTouch(0).position.y));
             pos.x = Mathf.Clamp(pos.x,-8.5,-4.5);
             pos.y = Mathf.Clamp(pos.y,-2.25,1.75);
                 transform.position = new Vector2(pos.x, pos.y);
            }
     }

description of the issue heart smiley face and star

-heart must move inside the 1/4 of the circle freely
-if touch position is outside the circle zone it must move to closest position to that touch position (star)
- the center of the movement must be smiley face clearly.

i tried many solutions but all had errors probably it was my mistake as I am new to the awesome Unity3d scripting.

heart-range.jpg (21.8 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 MuradD · Mar 14, 2016 at 09:37 AM

I solved this issue and here is my code;

 using UnityEngine;
 using System.Collections;
 
 public class CorsairMover : MonoBehaviour {
 
 
     RaycastHit hit;
     public Transform Tank;
     public Transform corsair;
     //private Transform temp;
     float TankX;
     float TankY;
     float TankX2;
     float TankY2;
 
     void Start () {
         TankX = Tank.position.x;
         TankY = Tank.position.y;
         TankX2 = TankX + 4;
         TankY2 = TankY + 4;
     }
     void Update () {
          Ray2D x = new Ray2D(Tank.position, Input.mousePosition - Camera.main.WorldToScreenPoint(Tank.position));
         Debug.Log("Ray Origin : " + x.origin + " direction : " + x.direction);
         float distanceFromCanon = Vector2.Distance(Input.mousePosition, Camera.main.WorldToScreenPoint(Tank.position)) * 0.03f;
         Debug.Log(distanceFromCanon.ToString());
         distanceFromCanon = Mathf.Clamp(distanceFromCanon, 0f, 4f);
 
         //corsair.position = x.GetPoint(distanceFromCanon);
     Vector2 temp = x.GetPoint(distanceFromCanon);
         temp.x = Mathf.Clamp(temp.x, TankX, TankX2);
         temp.y = Mathf.Clamp(temp.y, TankY, TankY2);
         corsair.position = temp;
     }
     }


and I am using static position for my tank thats why if it moves it will only take initial position as clamping reference; // not tested but you can move start() function contents to update to make it follow tank.position

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Factorio-like sprite directions 0 Answers

"Invalid argument"? 0 Answers

Player Not Moving With Platform 2D 1 Answer

How can i do an action after i detect a collision2D && and a key/mouse button being pressed? 1 Answer

I want to rotate my character using analog stick. I have set up rotating towards mouse cursor 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