I'm making a game where I put the ball into the goal by rotating the stage.
When you rotate the stage, smooth moving black obstacles sink and float (probably because of Vector 3).
The solution to this is how to fix the distance between the stage and the obstacle
I would like to know (the obstacle does not float or sink when the stage is rotated).
Moving Obstacles
using System.Collections;
using System.Collections.General;
using UnityEngine;
public class RepeatMovementOfShark: MonoBehavior
{
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
public bool FreezeY=false;
private Vector3 startPos;
floatway=1.0f;
float zPos = 0.0f;
float zPosPrev = 0.0f;
void Start()
{
// save start position
startPos = transform.position;
}
void Update()
{
Vector3 currentPos = transform.position;
// advance position
zPosPrev = zPos;
zPos+=way*zSpeed*Time.deltaTime;
// handle turn around at end
if (Mathf.Abs(zPos)>zDistance)
{
way=-way;
zPos = zPosPrev;
}
// handle turnaround at start
if((zPos<0.0f&&zPosPrev>0.0f)||(zPosPrev<0.0f&&zPos>0.0f))
{
way=-way;
zPos = zPosPrev;
}
if (FreezeY)
{
currentPos.y = startPos.y;
}
// set new position
transform.position = new Vector3 (transform.position.x, transform.position.y, startPos.z+zPos);
}
}
Rotating the Stage
using System.Collections;
using System.Collections.General;
using UnityEngine;
// Transform.rotation example.
// Rotate a GameObject using a Quaternion.
// Tilt the cube using the arrow keys. When the arrow keys are released
// The cube will be rotated back to the center using Slerp.
public class StageRotation—MonoBehavior
{
public float smooth=5.0f;
public floattiltAngle=60.0f;
void Update()
{
// Smoothlytilts a transform towers a target rotation.
floattiltAroundZ=Input.GetAxis("Horizontal")*tiltAngle*-1;
floattiltAroundX=Input.GetAxis("Vertical")*tiltAngle*-1;
// Rotate the cube by converting the angles into a quatern
Quaternion target=Quaternion.Euler(tiltAroundX,0,tiltAroundZ);
// Dampen towers the target rotation
transform.rotation=Quaternion.Slerp(transform.rotation, target, Time.deltaTime*smooth);
}
}
Attached Files:
c# unity3d
Aren't the parents of the stage and the parents of the obstacle the same in hierarchy?
By staging the parent of an obstacle, you can follow the rotation of the stage without updating the coordinates of the obstacle.
© 2024 OneMinuteCode. All rights reserved.