problems with the gun, Making a Multiplayer FPS in Unity Ep.2

Updated on August 31, 2018 in Unity
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on August 28, 2018
hi, can someone help me please?
when i’m rotating the camera up and down, it stretches my gun weirdly…
please help!!!!!!
  • Liked by
Reply
2 on August 29, 2018

Did you parent the gun object correctly to the player, and where is the rotating weird? Client or server side?

on August 29, 2018

This is the hierarchy:

And here the gun is fine:

 

But when i’m rotating up or down:

 

Helpful
on August 30, 2018

I think your rotation by scripting is causing the issue, try to check the scripts for that.

Show more replies
  • Liked by
Reply
Cancel
1 on August 30, 2018

can someone help me??

Helpful
on August 30, 2018

Can you post the script you have and a link of the youtube tutorial with a timestamp when you are facing this issue?

 

Show more replies
  • Liked by
Reply
Cancel
0 on August 31, 2018

This is the controller code:

using UnityEngine;
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour {
[SerializeField]
 private float speed = 10f;
 [SerializeField]
 private float mouseSensitivity = 3f;
private PlayerMotor motor;
private void Start()
 {
 motor = GetComponent<PlayerMotor>();
 }
private void Update()
 {
 float xMov = Input.GetAxisRaw("Horizontal");
 float zMov = Input.GetAxisRaw("Vertical");
Vector3 movHorizontal = transform.right * xMov;
 Vector3 movVertical = transform.forward * zMov;
//Final movement
 Vector3 velocity = (movHorizontal + movVertical).normalized * speed;
//Apply movement
 motor.Move(velocity);
 //Get rotation
 float yRot = Input.GetAxisRaw("Mouse X");
//Calculate ratation
 Vector3 rotation = new Vector3(0f, yRot, 0f) * mouseSensitivity;
//Apply rotation
 motor.Rotate(rotation);
//Get camera rotation
 float xRot = Input.GetAxisRaw("Mouse Y");
//Calculate camera ratation
 Vector3 cameraRotation = new Vector3(xRot, 0f, 0f) * mouseSensitivity;
//Apply rotation
 motor.RotateCamera(cameraRotation);

And this is the motor:

using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour {
private Vector3 velocity = Vector3.zero;
 private Vector3 rotation = Vector3.zero;
 private Vector3 cameraRotation = Vector3.zero;
[SerializeField]
 private Camera cam;
private Rigidbody rb;
private void Start()
 {
 rb = GetComponent<Rigidbody>();
 }
//Get movement vector
 public void Move(Vector3 _velocity)
 {
 velocity = _velocity;
 }
//Get rotation movement from controller
 public void Rotate(Vector3 _rotation)
 {
 rotation = _rotation;
 }
//Get camera rotation movement from controller
 public void RotateCamera(Vector3 _cameraRotation)
 {
 cameraRotation = _cameraRotation;
 }
void FixedUpdate()
 {
 DoMovement();
 DoRotation();
 }
//make movement by vector
 void DoMovement()
 {
 if (velocity != Vector3.zero)
 rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
void DoRotation()
 {
 rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
 if (cam != null)
 {
 cam.transform.Rotate(-cameraRotation);
 }
 }
}

And the tutorial:

https://youtu.be/kjUiARseG18?t=15m16s

Thank you!

  • Liked by
Reply
Cancel