Question
First Person Shooter in Unity. I am following a tutorial that is written in Javascript, and I am trying to translate it into C#. I
First Person Shooter in Unity. I am following a tutorial that is written in Javascript, and I am trying to translate it into C#. I mostly have it all functioning, except for the "holdHeight" and "holdSide" public variables in the Inspector. I can get them to show up, but when you adjust them in Unity, they do nothing to the position of the gun. I think it's because they are not being read into the "new" Vector3 created in the first line, transform.position. Here is my C# code:
using UnityEngine;
public class GunScript : MonoBehaviour { public GameObject cameraObject; // Parameter for the Main Camera Object in Unity [HideInInspector] public float targetXRotation; [HideInInspector] public float targetYRotation; [HideInInspector] public float targetXRotationV; [HideInInspector] public float targetYRotationV;
public float rotateSpeed = 0.3f; // Shows Up in Inspector But Adjustment Does Nothing public float holdHeight = -0.5f; // Shows Up in Inspector But Adjustment Does Nothing public float holdSide = 0.5f; // Shows Up in Inspector But Adjustment Does Nothing
// Update Is Called Once Per Frame void FixedUpdate() { // Assign the Camera Game Object in Unity to the Gun and Allow Adjustment of Gun's Position transform.position = cameraObject.transform.position + (Quaternion.Euler(0, targetYRotation, 0) * new Vector3 (holdSide, holdHeight, 2)); // *** This Is Where I Think The Error Is // Allow Gun to Move in Player's "Hand" targetXRotation = Mathf.SmoothDamp(targetXRotation, cameraObject.GetComponent
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started