- Home /
how do i add a offset to my sword rotation?
I have a 1st person and my sword is supposed like the guns you see in fpses but I've tried to add an offset to the sword position becuase all I see is the sword's pommel and guard(I don't want to change the pivot point of the sword because I want to add animations to it) but the offset only changes the point where the sword rotates around. What should I do so that it rotates around the player but allows me to add a offset so my sword's position is offset but not the point of rotation
my camera script
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseCameraScript : MonoBehaviour
{
CursorLockMode lockerd = CursorLockMode.Locked;
void Start()
{
Cursor.lockState = lockerd;
}
//^ just locks the mouse
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
public float length;
public Vector3 SwordPos;
public Transform Sword;
public Vector3 SwordOffset;
void Update()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(transform.position, Camera.main.transform.forward * length, Color.black);
//v This makes the point where the sword rotates around
SwordPos = ray.origin + (ray.direction * length);
SwordPos = ray.GetPoint(length);
//^
Sword.position = SwordPos;
Debug.Log(ray.origin);
//^Main problem area(there might be a problem below but idk)
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}
Answer by antx · May 03, 2017 at 01:18 PM
If I understood you right, then the way to go in such cases is to use an empty gameObject as a proxy. In your Hierarchy put that proxy gameObject where the sword would be and then make the sword a child of that proxy. Now move your sword to the offset you want relative to the proxy.
If you rotated(or what else) your sword via script before, do that with the proxy instead.
Answer by Piyush_Pandey · May 03, 2017 at 01:16 PM
0) Note down the swords position.
1) Create an empty parent
2)Assign sword as its child. Set the position as Vector3(0,0,0) of the sword.
3) Now assign this new sword parent exactly the same spot {and hierarchy} you placed for the sword. (from the point 0 above)
4) Now take the sword {not its parent } and place it wherever you want with offset.
The sword pivot is not changed. Any rotation applied to it will be on its localPosition.
Your answer
Follow this Question
Related Questions
(URGENT) I have a swinging axe I can get to rotate once in 1 direction but i need it to loop 3 Answers
Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer
How to rotate my player with the gamepad right stick ? 0 Answers
[C#] Quaternion Rotations with Input.GetAxis problems. 1 Answer