Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TimidestShadow · May 13, 2020 at 05:58 PM · camera-movementplayer movementthird-person-camera

How can I fix the arm movement with the camera.

Trying to make a third person shooter and the goal right now is to get the arm (which a cylinder) to aim up and down. Right now, when the game starts, the cylinder position resets vertically but does aim with the camera but it offset really weird. Can someone please help me fix this? Im new to unity and am doing my best to learn

//Code for the arm

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Arm : MonoBehaviour { public float mouseSensitivity = 100f;

 public Transform arm;

 float xRotation = 0f;

 // Start is called before the first frame update
 void Start()
 {
     //Cursor.lockState = CursorLockMode.Locked;
     arm.transform.eulerAngles = new Vector3(arm.transform.eulerAngles.x, arm.transform.eulerAngles.y, arm.transform.eulerAngles.z);
 }

 // 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);
     arm.Rotate(Vector3.up * mouseX);
 }

}

//Code for the Camera

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);
 }

}

// Code for the player movement

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMovement : MonoBehaviour {

 CharacterController characterController;

 public float speed = 3.0f;
 public float jumpSpeed = 8.0f;
 public float gravity = 20.0f;

 public float shootSpeed = 20;

 private Vector3 moveDirection = Vector3.zero;

 public GameObject laserPrefab;
 public GameObject firePoint;

 // Start is called before the first frame update
 void Start()
 {
     transform.position = new Vector3(-5, 1, -10);
     characterController = GetComponent<CharacterController>();
 }

 // Update is called once per frame
 void Update()
 {

     if (characterController.isGrounded)
     {
         // We are grounded, so recalculate
         // move direction directly from axes

         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
         moveDirection *= speed;

         if (Input.GetButton("Jump"))
         {
             moveDirection.y = jumpSpeed;
         }
     }

     if (Input.GetMouseButtonDown(0))
     {
         Shoot();
     }

         // Player moves forward in direction of camera
         Vector3 move = transform.right * moveDirection.x + transform.forward * moveDirection.z;
     characterController.Move(move * speed * Time.deltaTime);


     // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
     // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
     // as an acceleration (ms^-2)
     moveDirection.y -= gravity * Time.deltaTime;

     // Move the controller
     characterController.Move(moveDirection * Time.deltaTime);
 }

 // Action to shoot
 void Shoot()
 {
     GameObject laser = Instantiate(laserPrefab, firePoint.transform.position, firePoint.transform.rotation);
     Rigidbody rb = laser.GetComponent<Rigidbody>();
     rb.AddForce(transform.forward * shootSpeed, ForceMode.Impulse);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by darksider2000 · May 13, 2020 at 06:32 PM

Hi @TimidestShadow

First off your code in Start() is redundant:

 arm.transform.eulerAngles = new Vector3(arm.transform.eulerAngles.x, arm.transform.eulerAngles.y, arm.transform.eulerAngles.z);



So I skimmed through your post and what I think is causing your offset is the fact that you're using Transform.localRotation

If your cylinder has a parent (probably the character object/model) then localRotation will rotate it in local space relative to the parent. Instead of world space.


Give it a shot with Transform.rotation instead, and you shouldn't have any offset then.

 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.rotation = Quaternion.Euler(xRotation, 0f, 0f);
      arm.Rotate(Vector3.up * mouseX);
  
 }


If this doesn't fix your issue try taking a screenshot of the offset if you can, that'd help single it out.

Gluck!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TimidestShadow · May 13, 2020 at 07:01 PM 0
Share

This issue is that when the game starts, the cylinder acting as the arm, resets to a up and down orientation when I start the game ins$$anonymous$$d of staying extended from the body. It will move with the camera but in order to fire straight, I have to aim the camera at the ground. CorrectPosition

correctposition-2.png (313.7 kB)
avatar image TimidestShadow · May 13, 2020 at 07:01 PM 0
Share

GameStart

gamestart-2.png (298.9 kB)
avatar image TimidestShadow · May 13, 2020 at 07:01 PM 0
Share

Problem

problem-2.png (204.8 kB)
avatar image darksider2000 TimidestShadow · May 13, 2020 at 07:31 PM 1
Share

Ok sure I see what you mean.

This is how a regular cylinder in unity is created. The ends of the cylinder are on the Y axis, but you're ai$$anonymous$$g the Z axis at the target.


There's a quick and easy fix and that's just to add an extra 90 degrees of rotation to the X axis whenever you calculate the cylinder's rotation.

So:

 transform.rotation = Quaternion.Euler(xRotation - 90, 0f, 0f); // -90 or +90 depending on if you want the transform.forward or -transform.forward pointing at the target



But if you're planning on adding graphical assets later on such as weapon models etc, I'd suggest you create an empty GameObject to handle the arm/weapon's code and put the mesh object (Cylinder in this case) as a child under the empty GameObject.


This will give you a lot of flexibility over the visual aspect without affecting the code behavior. For example: A rifle's recoil animation would move the gun model but not the parent, and so would not affect the alignment with camera view.

avatar image TimidestShadow · May 13, 2020 at 07:59 PM 0
Share

Thank you so much!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving Player Relative to Camera? 1 Answer

Can somebody help me with a TPS Camera? 1 Answer

Player character becomes shaky when camera is moving 0 Answers

So how do I rotate my camera to my player 1 Answer

Camera movement in a Third Person Shooter Game 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges