- Home /
Question by
kevtrc15 · Apr 25, 2020 at 05:58 AM ·
collisiontransformbeginnertransform.positionnewbie
transform.position returning wrong value (UNITY 2D)
I am trying to place the ball on the paddle until a key is pressed, also there is box collider that triggers if the ball falls down. For some reason the transform.position.y of the ball gameObject is going below zero while the ball is still stuck to the paddle in the game and therefore triggering the collider unnecessarily . Attaching code of paddle and ball bellow
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ballScript : MonoBehaviour
{
[SerializeField] GameObject myPaddle;
Vector3 paddleToBallVector;
void Start()
{
paddleToBallVector = transform.position - myPaddle.transform.position;
lockToPaddle();
}
void Update()
{
lockToPaddle();
}
private void lockToPaddle()
{
transform.position = myPaddle.transform.position + paddleToBallVector;
}
}
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class paddleScript : MonoBehaviour
{
[SerializeField] float screenWidthUnits = 16f;
[SerializeField] float paddleLeftLimit = 1.07f;
[SerializeField] float paddleRightLimit = 14.928f;
void Update()
{
float screenWidthPercentage = Input.mousePosition.x / Screen.width;
Vector2 curPaddlePos = new Vector2(screenWidthPercentage * screenWidthUnits, transform.position.y);
curPaddlePos.x = Mathf.Clamp(curPaddlePos.x, paddleLeftLimit, paddleRightLimit);
transform.position = curPaddlePos;
}
}
screenshot-72.jpg
(405.8 kB)
Comment