- Home /
I still have the question that is not solved yet.
I know that it is against the rule of unity answer system but my problem isn't solve yet, therefore I just deleted previous post about similar question and I have to get answer for my problem.
My problem is that my obstacle in my game(which is rotating stage and move ball into a goal) is still floating or sinking when I rotate my stage, even I put my obstacle as child of stage. I already found out there 2(or 3) problems.
1: When I remove the programmed code of C#, there is no sinking or floating of obstacle anymore , therefore the problem must be in the my programmed code, and when I asked same question at the stack overflow, it says that "There is no requirement of updating the position of obstacle", but what is other way tp calculate vector of my obstacles?
2: When I talk this problem to my friend, he said that there might be some "edge case problem", but when I research this "edge case", I have no idea how I can solve this(I know it is vert bad thing that I need to apologize to him...) to my programmed code.
3 : When I asked same question to other webpages, (I think It was Unity Forum) It said that I should use Rigidbody.MovePosition, but Also, I have no idea how can I use although I researched about this.
I want to know these three questions to solve my problems of my child object is keeping floating or sinking.
Code for movement of obstacle
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RepeatMovementOfShark : MonoBehaviour
{
public float zSpeed = -1.0f; //units per second, set negative to go the other way
public float zDistance = 10.0f; //distance to travel before turning back
private Vector3 startPos;
float way = 1.0f;
float zPos = 0.0f;
float zPosPrev = 0.0f;
float Distance;
public Rigidbody Stage;
public Rigidbody Shark;
void Start()
{
//save start position
startPos = transform.position;
}
void Update()
{
Movement();
//handle turn around at end
if (Mathf.Abs(zPos) > zDistance)
{
way = -way;
zPos = zPosPrev;
}
//handle turn around at start
if ((zPos < 0.0f && zPosPrev > 0.0f) || (zPosPrev < 0.0f && zPos > 0.0f))
{
way = -way;
zPos = zPosPrev;
}
var SharkYPosition = Shark.position.y;
var StageYPosition = Stage.position.y;
Distance = SharkYPosition - StageYPosition;
Debug.Log(Distance);
}
private void Movement()
{
Vector3 currentPos = transform.position;
//advance position
zPosPrev = zPos;
zPos += way * zSpeed * Time.deltaTime;
//set new position
transform.position = new Vector3(transform.position.x, transform.position.y, startPos.z + zPos);
}
}
I want any solution for these three questions to solve my obstacle stop sinking and floating by fixing the C# code above.
After I play the game , my obstacle starts sinking and floating like the picture above, I want to remain the distance between obstacle and stages all the time like picture shown below.