This question was
closed Jun 12, 2020 at 05:53 PM by
TwiningLion1357 for the following reason:
The question is answered, right answer was accepted
Question by
TwiningLion1357 · Jun 12, 2020 at 04:11 AM ·
c#lightingspotlightfirst personlooking
first person spotlight look
Hello, I have a first person player with a fully functioning mouse looking and movement + jumping script. I also have a spotlight attached to the player. What I what to do, is wherever the mouse looks, the spotlight will look to. If anyone can help me, It would be GREATLY appreciated.
Comment
Best Answer
Answer by TwiningLion1357 · Jun 12, 2020 at 05:52 PM
For everyone following this question, I have figured it out. Here is the code I used:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}