Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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().xRotation, ref targetXRotationV, rotateSpeed); targetYRotation = Mathf.SmoothDamp(targetYRotation, cameraObject.GetComponent().yRotation, ref targetYRotationV, rotateSpeed); // Allow Gun to Move With Player's "Arm" transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

2nd Edition

0471180742, 978-0471180746

More Books

Students also viewed these Databases questions

Question

How do you add two harmonic motions having different frequencies?

Answered: 1 week ago