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 LeviTM · Oct 26, 2021 at 10:39 PM · collisiontransformcollision detectiontransform.position

How Can I Teleport a Parent Object to its Child?

Hi everyone, I'm currently writing a script in a golfing game. The main script I'm focusing on now is the collision handler for the golf ball, and what I want to happen here is after the ball lands on the ground(terrain), the player model will teleport to the ball's position added onto the distance that was separating the two from the beginning. In other words, I don't want the player model to teleport directly on top of the ball.

I have some lines of code that do this same thing with the ball teleporting to the player model(as seen on lines 22, 29, and 94), but for some reason, I can't get it to work the other way around. The ball is childed under the player model prefab, which may help make this situation easier.

This script is referencing my Player1Controls script, so I will include that as it may be relevant.

I'm aware my code is probably pretty messy, so I apologize in advance if that is the case. This is the first game I'm making in Unity. Another thing to note is that "this.CallWithDelay()" should be treated as an invoke method.

For some reason, not all of my code is being detected by this post, and it's not being separated from my text. Sorry if the line numbers are misaligned.

using System; using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CollisionHandler : MonoBehaviour {

 public PlayerControls playerControls;
 public ScoreValue scoreValue;
 public Player1Controls player1Controls;

 CollisionHandler ch;
 Rigidbody rb;

 public GameObject playerObj;

 public bool isReset = false;
 float ballDelay = 0.5f;
 float delay = 5f;
 public Vector3 relativePosition;
 public Vector3 reset_relativePosition;

 void Start()
 {
     ch = GetComponent<CollisionHandler>();
     rb = GetComponent<Rigidbody>();
     playerObj = GameObject.FindGameObjectWithTag("Player1");
     reset_relativePosition = transform.localPosition;
 }

 void Update()
 {
     

     
 }
 private void OnCollisionEnter(Collision collision)
 {
     if(playerControls.isLaunched)
     {
         if (collision.gameObject.tag == "Terrain")
         {
             this.CallWithDelay(PlayerTeleport, delay);
         }
     }
 }


 private void OnTriggerEnter(Collider water)
 {
     if(water.gameObject.tag == "Water")
     {
         this.CallWithDelay(Retry, ballDelay);
         this.CallWithDelay(Reset, ballDelay);
         if (isReset)
         {
             transform.rotation = Quaternion.Euler(0, 0, 0);
             isReset = false;
         }
     }
 }

 public void PlayerTeleport()
 {
     relativePosition = transform.localPosition;
     player1Controls.transform.localPosition = relativePosition;
 }

 public void Retry()
 {
     
     isReset = true;

     if (playerControls.isAerial)
     {
         playerControls.isAerial = false;
     }

     if (playerControls.isTransitioning)
     {
         playerControls.isTransitioning = false;
     }

     if (scoreValue.Force_UIText.enabled == false)
     {
         scoreValue.Force_UIText.enabled = true;
     }

     if (playerControls.isLaunched)
     {
         playerControls.isLaunched = false;
     }

     transform.localPosition = reset_relativePosition;
     rb.constraints = RigidbodyConstraints.FreezePosition;
 }
 void Reset()
 {
     if(rb.constraints == RigidbodyConstraints.FreezePosition)
     {
         rb.constraints = RigidbodyConstraints.None;
         isReset = true;
     }
 }

}

using System; using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Player1Controls : MonoBehaviour {

 public PlayerControls playerControlsScript;

 float yRotate = 0f;
 [SerializeField]
 float rotateSpeed = 30f;

 [Header("Clamping stuff")]
 [Tooltip("Far left clamp")]
 [SerializeField]
 float MinClamp = -70f;
 [Tooltip("Far right clamp")]
 [SerializeField]
 float MaxClamp = 70f;

 private void Start()
 {
     playerControlsScript.isLaunched = false;
 }


 void Update()
 {
     if (!playerControlsScript.isLaunched)
     {
         LookAround();
     }
 }

 private void LookAround()
 {
     if(this.gameObject.tag == "Player2") { return; }
     if (Input.GetKey(KeyCode.A))
     {
         if (yRotate < MinClamp) { return; }
         yRotate += -rotateSpeed * Time.deltaTime;
         //transform.Rotate(0, -yRotate, 0);
         //yRotate = Mathf.Clamp(MinClamp, yRotate, MaxClamp);
         transform.rotation = Quaternion.Euler(0.0f, yRotate, 0.0f);
     }

     if (Input.GetKey(KeyCode.D))
     {
         if (yRotate > MaxClamp) { return; }
         yRotate += rotateSpeed * Time.deltaTime;
         //transform.Rotate(0, -yRotate, 0);
         //yRotate = Mathf.Clamp(MinClamp, yRotate, MaxClamp);
         transform.rotation = Quaternion.Euler(0.0f, yRotate, 0.0f);
     }
 }

}

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

264 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Unable to get Player to stay on Moving Platform, Collision not being detected. 0 Answers

Player Transform Postion on Collision Javascript/C# 0 Answers

Detecting collision altho disabled on matrix. Help!! :'( 0 Answers

OnTriggerEnter doesn't work if the colliding object is not moving. Translating it by (0,0,0) fixes it. What's the problem? 4 Answers

Compatible collision constraint methods 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