为什么当我将脚本附加到手电筒时没有任何反应?
Why when i attach a script to the flashlight nothing happen ?
我想做的是当手电筒打开时可以用鼠标旋转手电筒。但是当我将脚本附加到手电筒时,我没有收到任何错误,只是当我移动鼠标时什么也没有发生。我试图将脚本附加到 GameObject 我也尝试将脚本附加到 EthanRightHand 但没有。
但是如果我将脚本附加到 ThirdPersonController,它将旋转角色。但是我只想在手电筒打开时旋转它,或者在手电筒打开时旋转 EthanRightHand 可能会更好。
我可以将第二个脚本中的手电筒设置为 public 静态。但这不是重点。问题是 ObjectRotator 仅在 ThirdPersonController 上工作的第一个脚本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectRotator : MonoBehaviour
{
int speed;
float friction;
float lerpSpeed;
private float xDeg;
private float yDeg;
private Quaternion fromRotation;
private Quaternion toRotation;
// Use this for initialization
void Start()
{
speed = 3;
friction = 3f;
lerpSpeed = 3f;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
xDeg -= Input.GetAxis("Mouse X") * speed * friction;
yDeg += Input.GetAxis("Mouse Y") * speed * friction;
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg, xDeg, 0);
transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);
}
}
}
还有手电筒脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class Flashlight : MonoBehaviour
{
[SerializeField]
Transform someBone;
Light flashlight;
// Use this for initialization
void Start()
{
flashlight = GameObject.FindGameObjectWithTag("Flashlight").GetComponent<Light>();
transform.parent = someBone;
}
// Update is called once per frame
void Update()
{
transform.localRotation = someBone.localRotation;
transform.localScale = someBone.localScale;
if (Input.GetKeyDown(KeyCode.F))
{
if (flashlight.enabled)
{
flashlight.enabled = false;
}
else
{
flashlight.enabled = true;
}
}
}
}
如果您要使 FlashLight
成为 Hand
的子项,您需要从代码中删除这两行
transform.localRotation = someBone.localRotation;
transform.localScale = someBone.localScale;
因为这将旋转和缩放 FlashLight
以及手。
我想做的是当手电筒打开时可以用鼠标旋转手电筒。但是当我将脚本附加到手电筒时,我没有收到任何错误,只是当我移动鼠标时什么也没有发生。我试图将脚本附加到 GameObject 我也尝试将脚本附加到 EthanRightHand 但没有。
但是如果我将脚本附加到 ThirdPersonController,它将旋转角色。但是我只想在手电筒打开时旋转它,或者在手电筒打开时旋转 EthanRightHand 可能会更好。
我可以将第二个脚本中的手电筒设置为 public 静态。但这不是重点。问题是 ObjectRotator 仅在 ThirdPersonController 上工作的第一个脚本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectRotator : MonoBehaviour
{
int speed;
float friction;
float lerpSpeed;
private float xDeg;
private float yDeg;
private Quaternion fromRotation;
private Quaternion toRotation;
// Use this for initialization
void Start()
{
speed = 3;
friction = 3f;
lerpSpeed = 3f;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
xDeg -= Input.GetAxis("Mouse X") * speed * friction;
yDeg += Input.GetAxis("Mouse Y") * speed * friction;
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg, xDeg, 0);
transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);
}
}
}
还有手电筒脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class Flashlight : MonoBehaviour
{
[SerializeField]
Transform someBone;
Light flashlight;
// Use this for initialization
void Start()
{
flashlight = GameObject.FindGameObjectWithTag("Flashlight").GetComponent<Light>();
transform.parent = someBone;
}
// Update is called once per frame
void Update()
{
transform.localRotation = someBone.localRotation;
transform.localScale = someBone.localScale;
if (Input.GetKeyDown(KeyCode.F))
{
if (flashlight.enabled)
{
flashlight.enabled = false;
}
else
{
flashlight.enabled = true;
}
}
}
}
如果您要使 FlashLight
成为 Hand
的子项,您需要从代码中删除这两行
transform.localRotation = someBone.localRotation;
transform.localScale = someBone.localScale;
因为这将旋转和缩放 FlashLight
以及手。