我正在做一个滚球比赛,球在球上移动,当球与立方体碰撞时它会计数。每次球移动时我都需要对比赛进行计数,但我似乎无法弄明白。我一直在收到错误。我感谢任何帮助。 使用UnityEngine;
#include <stdlib.h>
#include <stdio.h>
#include <x86intrin.h>
#include <time.h>
#ifndef __cplusplus
#include <stdalign.h> // C11 defines _Alignas(). This header defines alignas()
#endif
#define CYCLE_COUNT 10000
// add vector and return resulting value on stack
__attribute__((noinline)) __m128i add_iv(__m128i *a, __m128i *b) {
return _mm_add_epi32(*a,*b);
}
// add int vectors via sse
__attribute__((noinline)) void add_iv_sse(__m128i *a, __m128i *b, __m128i *out, int N) {
for(int i=0; i<N/sizeof(int); i++) {
//out[i]= _mm_add_epi32(a[i], b[i]); // this also works
_mm_storeu_si128(&out[i], _mm_add_epi32(a[i], b[i]));
}
}
// add int vectors without sse
__attribute__((noinline)) void add_iv_nosse(int *a, int *b, int *out, int N) {
for(int i=0; i<N; i++) {
out[i] = a[i] + b[i];
}
}
__attribute__((noinline)) void p128_as_int(__m128i in) {
alignas(16) uint32_t v[4];
_mm_store_si128((__m128i*)v, in);
printf("int: %i %i %i %i\n", v[0], v[1], v[2], v[3]);
}
// print first 4 and last 4 elements of int array
__attribute__((noinline)) void debug_print(int *h) {
printf("vector+vector:begin ");
p128_as_int(* (__m128i*) &h[0] );
printf("vector+vector:end ");
p128_as_int(* (__m128i*) &h[32764] );
}
int main(int argc, char *argv[]) {
int n = atoi (argv[1]);
printf("n: %d\n", n);
// sum: vector + vector, of equal length
int f[32768] __attribute__((aligned(16))) = {0,2,4};
int g[32768] __attribute__((aligned(16))) = {1,3,n};
int h[32768] __attribute__((aligned(16)));
f[32765] = 33; f[32766] = 34; f[32767] = 35;
g[32765] = 31; g[32766] = 32; g[32767] = 33;
// https://stackoverflow.com/questions/459691/best-timing-method-in-c
clock_t start = clock();
for(int i=0; i<CYCLE_COUNT; ++i) {
add_iv_sse((__m128i*)f, (__m128i*)g, (__m128i*)h, 32768);
}
int msec = (clock()-start) * 1000 / CLOCKS_PER_SEC;
printf(" SSE Time taken: %d seconds %d milliseconds\n", msec/1000, msec%1000);
debug_print(h);
// process intense function again
start = clock();
for(int i=0; i<CYCLE_COUNT; ++i) {
add_iv_nosse(f, g, h, 32768);
}
msec = (clock()-start) * 1000 / CLOCKS_PER_SEC;
printf("NOSSE Time taken: %d seconds %d milliseconds\n", msec/1000, msec%1000);
debug_print(h);
return EXIT_SUCCESS;
}
答案 0 :(得分:5)
Unity中没有transform.location
这样的东西。我相信你想得到对象的位置。这是通过transform.position
完成的,并且可以使用transform.rotation
修改对象的旋转。
每次球移动时我都需要对比赛进行计数
您可以通过检查transform.position
何时更改来执行此操作。
以下是我根据上述内容所做的更改:
1 。将您的private float location;
更改为private Vector3 location;
2 。将location = transform.location;
更改为location = transform.position;
3 。将if (location != transform.location)
更改为if (location != transform.position)
4 。最后将location = transform.location;
更改为location = transform.position;
。
固定代码:
public class PlayerController : MonoBehaviour
{
// Create public variables for player speed, and for the Text UI game objects
public float speed;
public Text countText;
public Text winText;
public Text movesText;
// Create private references to the rigidbody component on the player, and the count of pick up objects picked up so far
private Rigidbody rb;
private int count;
private int moves;
private Vector3 location;
// At the start of the game..
void Start()
{
// Assign the Rigidbody component to our private rb variable
rb = GetComponent<Rigidbody>();
// Set the count to zero
count = 0;
// Set moves to zero
moves = 0;
// Run the SetCountText function to update the UI (see below)
SetCountText();
// Set the text property of our Win Text UI to an empty string, making the 'You Win' (game over message) blank
winText.text = "";
location = transform.position;
SetMovesText();
}
void LateUpdate()
{
if (location != transform.position)
{
moves = moves + 1;
location = transform.position;
SetMovesText();
}
}
// Each physics step..
void FixedUpdate()
{
// Set some local float variables equal to the value of our Horizontal and Vertical Inputs
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
// Create a Vector3 variable, and assign X and Z to feature our horizontal and vertical float variables above
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
// Add a physical force to our Player rigidbody using our 'movement' Vector3 above,
// multiplying it by 'speed' - our public player speed that appears in the inspector
rb.AddForce(movement * speed);
}
// When this game object intersects a collider with 'is trigger' checked,
// store a reference to that collider in a variable named 'other'..
void OnTriggerEnter(Collider other)
{
// ..and if the game object we intersect has the tag 'Pick Up' assigned to it..
if (other.gameObject.CompareTag("Pick Up"))
{
// Make the other game object (the pick up) inactive, to make it disappear
other.gameObject.SetActive(false);
// Add one to the score variable 'count'
count = count + 1;
// Run the 'SetCountText()' function (see below)
SetCountText();
}
}
// Create a standalone function that can update the 'countText' UI and check if the required amount to win has been achieved
void SetCountText()
{
// Update the text field of our 'countText' variable
countText.text = "Count: " + count.ToString();
// Check if our 'count' is equal to or exceeded 12
if (count >= 12)
{
// Set the text value of our 'winText'
winText.text = "You Win!";
}
}
void SetMovesText()
{
movesText.text = "Moves: " + moves.ToString();
}
}