Question

First Person Shooter in Unity. I am following a tutorial that is written in Javascript, and...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
First Person Shooter in Unity. I am following a tutorial that is written in Javascript, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hey I am having trouble with this problem in C++: The first race is tomorrow! You...

    Hey I am having trouble with this problem in C++: The first race is tomorrow! You are provided a list of race entries in the following form: vector<string> entryList = { "42, Vance Cardoza", "55, Vanessa Pandara", "36, Gaston Carlton", "99, Rich Diesel", "10, Euler Bustamente" }; In your main() function instantiate a Race object. Use a C++ iterator to walk through the entries in the list above, parse out each car number and driver name and add them to...

  • C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employee...

    C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employees to be compared by the Name, Age, and Pay, respectively. Code: Employee.Core.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Employees { partial class Employee { // Field data. private string empName; private int empID; private float currPay; private int empAge; private string empSSN = ""; private string empBene...

  • This is my code so far for the assignment. I am stuck on the last 3...

    This is my code so far for the assignment. I am stuck on the last 3 tasks which are task#3, task#4 and task#5. Task #3 String Comparisons In checkForDiscount() method of the driver: 1. Figure out if there is a discount of $2 on this order, due to the customer's name being either Mike or Diane 2. Return either 2 or 0 as the discount to the cost of the pizza 3. Compile, debug, and run. You should now be...

  • This is a repost of my question from before. I am having trouble writting the code...

    This is a repost of my question from before. I am having trouble writting the code for this project. Below is the project description with various methods and classes the code in C++ should implement. Project Description The goal of this project is to design and develop C++ code and algorithms to control a bank of elevators, which services many floors and transports people “efficiently ", that is, as per the given specifications. A second goal is to effectively employ...

  • In java language here is my code so far! i only need help with the extra...

    In java language here is my code so far! i only need help with the extra credit part For this project, you will create a Rock, Paper, Scissors game. Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer. If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors This is how the program works: The user clicks a button to make their move (rock, paper, or scissors). The program...

  • I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED str...

    I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED strips and 3 - ultrasonics. I have them working independently but I need to combine the code so they work together. Here is what I have: Memory Game #define PLAYER_WAIT_TIME 2000 // The time allowed between button presses - 2s byte sequence[100]; // Storage for the light sequence byte curLen = 0; // Current length of the sequence byte inputCount =...

  • Can somebody help me with the Use Case Diagram . I am confused of what I...

    Can somebody help me with the Use Case Diagram . I am confused of what I am suppose to do. Here are the instructions : Your team should produce a Use Case Diagram and the associated Use Case Descriptions/Narratives for all the use cases in the diagram. The resulting document should havethe “professional look” and produced by a word processor, graphics/presentation/drawing software, and/or a CASE tool (e.g., Microsoft Word, Microsoft PowerPoint, ArgoUML, Dia, Visual Paradigm, Visio, etc.). All project documentation...

  • C# - Inheritance exercise I could not firgure out why my code was not working. I...

    C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...

  • My Python file will not work below and I am not sure why, please help me...

    My Python file will not work below and I am not sure why, please help me debug! ********************************* Instructions for program: You’ll use these functions to put together a program that does the following: Gives the user sentences to type, until they type DONE and then the test is over. Counts the number of seconds from when the user begins to when the test is over. Counts and reports: The total number of words the user typed, and how many...

  • import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow...

    import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT