C# 如何在unity C中更新指定的向量变量#
我正在创建一个2D游戏,但如果发生什么事情,我似乎无法改变速度向量变量。我有一个倒计时的计时器变量,当它达到0时,应该改变speed-speed.x+=100和speed.y+=100,但它什么也不做。我知道倒计时是有效的,因为我已经用其他函数对它进行了测试C# 如何在unity C中更新指定的向量变量#,c#,unity3d,C#,Unity3d,我正在创建一个2D游戏,但如果发生什么事情,我似乎无法改变速度向量变量。我有一个倒计时的计时器变量,当它达到0时,应该改变speed-speed.x+=100和speed.y+=100,但它什么也不做。我知道倒计时是有效的,因为我已经用其他函数对它进行了测试 using System.Collections.Generic; using System.Linq; using UnityEngine; /// <summary> /// Parallax scrolling scri
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
/// <summary>
/// Parallax scrolling script that should be assigned to a layer
/// </summary>
public class Scrolling : MonoBehaviour
{
public float timeLeft = 5.0f;
/// <summary>
/// Scrolling speed
/// </summary>
public Vector2 speed = new Vector2(10, 10);
/// <summary>
/// Moving direction
/// </summary>
public Vector2 direction = new Vector2(-1, 0);
/// <summary>
/// Movement should be applied to camera
/// </summary>
public bool isLinkedToCamera = false;
/// <summary>
/// 1 - Background is infinite
/// </summary>
public bool isLooping = false;
/// <summary>
/// 2 - List of children with a renderer.
/// </summary>
private List<Transform> backgroundPart;
// 3 - Get all the children
void Start()
{
// For infinite background only
if (isLooping)
{
// Get all the children of the layer with a renderer
backgroundPart = new List<Transform>();
for (int i = 0; i < transform.childCount; i++)
{
Transform child = transform.GetChild(i);
// Add only the visible children
if (child.renderer != null)
{
backgroundPart.Add(child);
}
}
// Sort by position.
// Note: Get the children from left to right.
// We would need to add a few conditions to handle
// all the possible scrolling directions.
backgroundPart = backgroundPart.OrderBy(
t => t.position.x
).ToList();
}
}
void Update()
{
//Timer
timeLeft -= Time .deltaTime ;
if (timeLeft <= 0.0f)
{
speed.x += 100;
speed.y += 100;
}
// Movement
Vector3 movement = new Vector3(
speed.x * direction.x,
speed.y * direction.y,
0);
movement *= Time.deltaTime;
transform.Translate(movement);
// Move the camera
if (isLinkedToCamera)
{
Camera.main.transform.Translate(movement);
}
// 4 - Loop
if (isLooping)
{
// Get the first object.
// The list is ordered from left (x position) to right.
Transform firstChild = backgroundPart.FirstOrDefault();
if (firstChild != null)
{
// Check if the child is already (partly) before the camera.
// We test the position first because the IsVisibleFrom
// method is a bit heavier to execute.
if (firstChild.position.x < Camera.main.transform.position.x)
{
// If the child is already on the left of the camera,
// we test if it's completely outside and needs to be
// recycled.
if (firstChild.renderer.IsVisibleFrom(Camera.main) == false)
{
// Get the last child position.
Transform lastChild = backgroundPart.LastOrDefault();
Vector3 lastPosition = lastChild.transform.position;
Vector3 lastSize = (lastChild.renderer.bounds.max - lastChild.renderer.bounds.min);
// Set the position of the recyled one to be AFTER
// the last child.
// Note: Only work for horizontal scrolling currently.
firstChild.position = new Vector3(lastPosition.x + lastSize.x, firstChild.position.y, firstChild.position.z);
// Set the recycled child to the last position
// of the backgroundPart list.
backgroundPart.Remove(firstChild);
backgroundPart.Add(firstChild);
}
}
}
}
}
}
使用System.Collections.Generic;
使用System.Linq;
使用UnityEngine;
///
///应指定给图层的视差滚动脚本
///
公共类滚动:MonoBehavior
{
公共浮动时间限制=5.0f;
///
///滚动速度
///
公共矢量2速度=新矢量2(10,10);
///
///运动方向
///
公共向量2方向=新向量2(-1,0);
///
///应将移动应用于摄影机
///
public bool isLinkedToCamera=false;
///
///1-背景是无限的
///
公共bool isLooping=false;
///
///2-具有渲染器的子级列表。
///
私人名单背景部分;
//3-把所有的孩子都带走
void Start()
{
//仅适用于无限背景
如果(isLooping)
{
//使用渲染器获取层的所有子级
backgroundPart=新列表();
for(int i=0;it.position.x
).ToList();
}
}
无效更新()
{
//计时器
timeLeft-=时间增量时间;
如果(timeLeft mmh)我找不到任何错误。是否将公共变量设置为与脚本中设置的不同的值?出于某种原因发现问题,该变量必须设置为私有才能工作…您是否可以尝试使用[HideInInstit]公共向量2速度=新向量2(10,10);:)