Hello Unity Community. It’s been a while since I’ve asked a question here and here i’ve come again to a dead end in trying to solve what is a mystery to me. I have a weapon attack concept where the sword is controlled by the mouse Input.
I need the sword CUTTING side to always be aiming at the target. But at the SAME time I need to have freeflow movement on the Mouse X and Mouse Y.
I’ve taken a good look at quite a lot of examples from the Unity forums for independant but simultaneous rotations and also checked stackExchange and Googled the thing but i couldn’t find the answer as to HOW to
mix 2 different rotations any differently than how i’ve been able to do it.
The following code is pretty much doing what I want but the rotation on the y Axis (-direction.y)
is not really convenient at high speed. It doesn’t follow the target precisely anymore. Is there anyway to combine 2 different rotations like 1 rotation is aiming the sword at the target and the other rotation is giving freeflow movement in X and Y except somehow the sharp edge of the sword always will be aiming at the target. It seems to be a complicated Quaternion or transform.eulerAngles thing which is why I need insight as to how to achieve this differently than how I achieved it. Also provided is a gif Image to show the swing.
float mouseVertical = Input.GetAxis("Mouse X");
float mouseHorizontal = Input.GetAxis("Mouse Y);
Vector3 direction = target.transform.position - sword.transform.position;
float speed = 10;
sword.transform.Rotate(new Vector3(mouseVertical, -direction.y , -mouseHorizontal) * Time.deltaTime * speed);
I am also using BlindAmMe cutting Mesh code from here



Hello guys I am still bringing this subject up because I need help. Here is a video I made showing the concept in action. Turn the volume down I didn’t knew bandicam would also record the music.
The control of the sword is made completely with the mouse Input. I use Final Ik from the asset store to have the right hand stay on the sword. The problem is I need the sword to be always aiming at a crosshair that is right in front of the player but have all other axis unlocked and ready for mouse Inputs. I got no clue on how to achieve that… right now the script look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class swordAttack1 : MonoBehaviour
{
public GameObject sword;
public Transform target;
public GameObject swordOriginalPos;
public Transform rightHand;
public float speed1 = 1.0f;
public float speed = 1.0f;
public float speed2 = 1.0f;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
Vector3 centerPt;
void Start()
{
centerPt = swordOriginalPos.transform.position;
transform.parent = rightHand.transform.parent;
}
void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 movement = new Vector3(-Input.GetAxisRaw("Mouse Y"), Input.GetAxisRaw("Mouse X"), 0) * Time.deltaTime * speed1;
Vector3 newPos = transform.position + movement;
transform.position = RotatePointAroundPivot(sword.transform.position, swordOriginalPos.transform.position, movement);
}
if (Input.GetMouseButton(1))
{
Vector3 targetDir = target.position - transform.position;
targetDir.y = 0;
float step = speed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);
//newDir.y = transform.position.y;
Vector3 rot = new Vector3(Input.GetAxisRaw("Mouse X"), 0, 0);
transform.rotation = Quaternion.Lerp(transform.rotation, transform.rotation * Quaternion.LookRotation(rot), speed2 * Time.deltaTime);
}
}
public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
{
return Quaternion.Euler(angles) * (point - pivot) + pivot;
}
}
i did not watch the videos but i think i understand , correct me if i am misguided .
you want the player to swing the sword with the mouse axis movements , secondly the movement should be clamped around the target reticle within a certain amount of restriction like angle limits , around the target in x and y ,i presume.
thirdly you want the blade’s cutting edge to always face towards the target , which is a point on the line/vector passing through the reticule .
i think the solution would start out by moving the arm with ik , whilst having the sword follow the hand , but limit the movement angles of the arm .
if you limit the angles of the arms , you have just one step left and that is to determine the target point , the target point could be a distance olong the ray which passes through the reticle , then you simply face/turn the sword down/cut direction, towards that target.
by splitting movement calculation from sword facing direction in two seperate pieces of functions like i would ,could perhaps be a better aproach ?
Hi wjbender. Thank you for answering. I was able to make the sword move with the Mouse Input as shown in the 2nd video of this post. I can move the sword up and down and rotate it on whichever axis but I can’t seem to make it rotate while at the same time having it pointed at the target on one axis only. This little code snippet of my second post is aiming the sword at the target but it locks all other axis which makes it impossible to rotate it with the mouse… That’s why I don’t use it in the final transform.rotation at line 45. Ill continue trying some stuff out. I guess I gotta just mix the line 45 with the code snippet but I just don’t know how. Thank you again for the help.
Vector3 targetDir = target.position - transform.position;
targetDir.y = 0;
float step = speed * Time.deltaTime
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F)
//newDir.y = transform.position.y;
using UnityEngine;
using System.Collections;
public class swordtest : MonoBehaviour
{
public GameObject sword;
public GameObject target;
public GameObject hand;
public float speed =10;
public float rotate_speed=10;
public float max_distance=1f;
void Start ()
{
sword=GameObject.CreatePrimitive(PrimitiveType.Cube);
sword.transform.localScale=new Vector3(0.1f,0.5f,2);
GameObject sharpside=GameObject.CreatePrimitive(PrimitiveType.Cube);
sharpside.transform.localScale=new Vector3(0.1f,0.1f,2);
sharpside.transform.position=new Vector3(0,-0.25f,0);
sharpside.renderer.material.color=Color.red;
sharpside.transform.parent=sword.transform;
target=GameObject.CreatePrimitive(PrimitiveType.Cylinder);
target.transform.localScale=new Vector3(0.2f,1,0.2f);
target.renderer.material.color=Color.yellow;
hand=GameObject.CreatePrimitive(PrimitiveType.Sphere);
hand.transform.localScale=new Vector3(0.5f,0.5f,0.5f);
hand.transform.position=new Vector3(0,0,-1f);
hand.renderer.material.color=Color.green;
sword.transform.parent=hand.transform;
}
void Update ()
{
Vector3 mov=new Vector3(Input.GetAxis ("Mouse X")*Time.deltaTime*speed,Input.GetAxis ("Mouse Y")*Time.deltaTime*speed,0);
hand.transform.position+=mov;
Vector3 clamped=hand.transform.position;
clamped.x=Mathf.Clamp(clamped.x,target.transform.position.x-max_distance,target.transform.position.x+max_distance);
clamped.y=Mathf.Clamp(clamped.y,target.transform.position.y-max_distance,target.transform.position.y+max_distance);
hand.transform.position=clamped;
Quaternion rot=new Quaternion();
rot.SetLookRotation(sword.transform.forward,-(target.transform.position-hand.transform.position).normalized);
sword.transform.rotation=Quaternion.Lerp(sword.transform.rotation,rot,rotate_speed*Time.deltaTime);
}
}
i tried doing quaternions for each rotation , and then multiplying them all together for the final rotation , i could not figure out the 3rd one so i created this script , throw it on a new gameobject into a scene and test it .
hope that helps somehow , i do not have ik abilities in my old unity sooo.
1 Like
sorry for the long time It took to reply. Dude I was sceptic you could have created a script that could work. But I gotta say I am amazed with the results. seriously. I mean the script that you posted makes it so that I can use the mouse on both x and Y axis to move the “sword” and it keeps it aimed directly at the target. Still there is one more step that I will need to incorporate which is rotate the sword on it’s pivot point or following your script have it rotate on a full 360 motion right at the “green ball”. But I’m not gonna ask you to do it for me. I’m first gonna study your script and see how you have been able to manipulate … while writing the last line i just noticed that rotating the green ball rotates all the parts of sword like I want it too and it keeps it aimed directly at the target… wow. you are a Genius. way better than me at rotations stuff. Ill try to learn from that. I cannot thank you enough. I will try and post a video soon if I incorporate it in my project for you to see. Prob post the whole project too. Well see.Thank you.
It’s just too bad I don’t have a VR Headset to test this out… Your script with FinalIK Hand Poses that stays on the sword wherever it rotates just makes it sooo real. Plus if you add a cutting script like the one I posted above it’s pretty much gonna end like this:
ninekorn
1 Like
i had a much more complicated conbogulated thing going and somehow ended up with that .
glad it helped you , everyone has something they need help on , i would not proclaim to be any genius but i thank you for the comment and being such a friendly member .
have fun.
1 Like