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);
}
}
}