Reputation: 507
What i want to do is when the flashlight is on to be able to rotate the flashlight around with the mouse. But when i attach the script to the Flashlight i'm not getting any errors just nothing happen when i move the mouse around. I tried to attach the script to the GameObject i also tried to attach the script to the EthanRightHand but nothing.
But if i will attach the script to the ThirdPersonController it will rotate the character. But i want to rotate only the flashlight when it's on or to make it maybe nicer to rotate the EthanRightHand when the flashlight is on.
I can make the flashlight in the second script to be public static. But that is not the point. The problem is the first script the ObjectRotator only work on the 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);
}
}
}
And the flashlight script
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;
}
}
}
}
Upvotes: 0
Views: 93
Reputation: 1815
If you are going to make your FlashLight
a child of the Hand
you would need to remove these two lines from your code
transform.localRotation = someBone.localRotation;
transform.localScale = someBone.localScale;
Since that will rotate and scale the FlashLight
as well as the hand.
Upvotes: 2