Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by Xprees · Feb 03 at 01:13 PM · gameobjects

,Making Player Stay on edge of a gameobject

alt textI am Trying to make a game where the player can stick to rotating disks and shoot off them to another. but sometimes the player doesnt stay on the edge but to a bit far from it. how do i solve this? also I want it so that I will not have to set values for every disk i place and instead have the code adapt itself to the disk it is currently stuck with also i do sorta have a solution and that is to find the width of the current disk the Player is attached with divide by 2 and do the same for the player add them and set the players x position to that but i cant find a to do it in code.

here is my code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public GameObject player;
     public GameObject disk;
     private bool isStart = true;
     private float rotationSpeed = float.PositiveInfinity;
     public float speed = 10;
     public Rigidbody2D playerB;
     public bool hascontrol = false;
     public SpriteRenderer diskSize;
     private Vector3 width;
      
 
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space) && isStart == true)
         {
 
             Mgo();
 
             isStart = false;
             hascontrol = true;
         }
     }
 
     public void Mgo()
     {
         Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
         Quaternion rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
 
         playerB.AddForce(transform.up * speed);
     }
 
     public void go()
     {
         
 
         playerB.AddForce(transform.up * speed);
     }
 
     private void FixedUpdate()
     {
 
         if (hascontrol == true && Input.GetKeyDown(KeyCode.Space))
         {
             Mgo();
             hascontrol = false;
             transform.SetParent(null);
         }
 
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     { 
        
 
         if (collision.gameObject.tag == "Disk")
         {
             
             player.transform.parent = collision.transform;
             hascontrol = true;
         }
     }
 
 }
 


my-project-samplescene-pc-mac-linux-standalone-uni.png (124.7 kB)
Comment
Add comment · Show 1
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
avatar image Caeser_21 · Feb 04 at 11:57 AM 0
Share

Can you send an image of the edge problem?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by progamerpagan819 · Feb 03 at 05:06 PM

As far as I understand you want so that the Player sticks to an object that spins and later is able to shoot it off the opposite direction so it can stick to another game object.

So what you will need is to get the disk gameObject component I already make this script that will need to apply to the player with the connection logic

 using UnityEngine;
 
 public class Player : MonoBehaviour
 {
     //SerializeField is use to keep the variable private but still available to see in the inspector.
     [SerializeField] private GameObject diskGameobject;
 
     //Using the RigidBody2D component into a variable.
     private Rigidbody2D playerRb;
 
     //This will be the force in with the player will be launch.
     private float launchForce = 5f;
     
     
     // Start is called before the first frame update
     void Start()
     {
         // Get the RigidBody2D component from the player.
         playerRb = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
         //Check if the player is in a disk and when he presses space it will be lunch the opposite direction.
         if (Input.GetKeyDown(KeyCode.Space) && diskGameobject != null)
         {
             //Detach the player from the  disk that is currently in.
             gameObject.transform.parent = null;
             
             //Add the force to launch the player to the opposite direction the the current disk that he's in.
             playerRb.AddForce(transform.position - diskGameobject.transform.position * launchForce, ForceMode2D.Impulse);
         }
     }
 
     //Trigger when the player collides with another collider.
     private void OnCollisionEnter2D(Collision2D disk)
     {
         //Check if the player collided with a Disk tagged collider.
         if (disk.gameObject.CompareTag("Disk"))
         {
             //Sleep the RigidBody2D so the player doesn't move from his position.
             playerRb.Sleep();
             
             //Set the variable "diskGameObject" to the new disk that the player collided with.
             diskGameobject = disk.gameObject;
             
             //Make the new disk that the player collided with a parent of player.
             gameObject.transform.parent = diskGameobject.transform;
         }
         else
         {
             //If the collision isn't tagged as "Disk" then it won't work.
             Debug.Log("The player collided with an object that isn't a disk or is not tagged as Disk.");
         }
     }
 }

Note that you will need to set the player to a disk by default at the start of the game so in the player script,alt textyou give it his gameObject and make it a parent.

If you have any questions about the script that I made, let me know.


example1.png (10.1 kB)
example2.png (13.4 kB)
Comment
Add comment · Show 2 · 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
avatar image progamerpagan819 · Feb 03 at 05:16 PM 0
Share

Another thing, the "Squares" are just a reference point because is hard to tell if an object is spinning when is a solid color.

avatar image Xprees · Feb 04 at 11:27 AM 0
Share

ok thats great and all but it doesnt fix the issue i came here for the player needs to stay on the edge of the disk but it still always doesnt do that

avatar image
0

Answer by Caeser_21 · Feb 04 at 02:26 PM

It might just be that the collider on either the disk or the player is too big. So when both of them collide the point of collision is away from the edge of the disk...

Comment
Add comment · Show 3 · 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
avatar image Xprees · Feb 04 at 03:22 PM 0
Share

no the colliders are good and they always dont stay the distance apart

avatar image Caeser_21 Xprees · Feb 04 at 03:57 PM 0
Share

is the 'disk' object an empty Gameobject?

avatar image Xprees Caeser_21 · Feb 05 at 08:41 AM 0
Share

no its not it has a circle collider sprite renderer

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

135 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

Related Questions

how to check all elements in array 2 Answers

Can u "bind" game objects together to increase performence in an explosion or traffic jam? 1 Answer

In a group of gameobjects, how to have only one object active at a time while disabling the others? 2 Answers

Having an issue with one script on multiple GameObjects 0 Answers

How do I make it so every object the player creates (in a building game) gets a script attach to it by default? 4 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