How to move rotated knife(RigidBody2D) diagonally?
Hello, I am new to Unity and so far enjoy my journey. Right now I have accomplished my knife constant rotation within [-30;30] degrees range. However, after the user presses any key, my knife should be moving fast in the direction it currently faces.
How can I achieve the following behavior? I tried addForce, changing velocity, but no results... Perhaps it is even impossible to do?
Here my knife is facing the left angle and I would like to it to just move in that direction really fast. No fancy effects :)
Here is base position
Here is my source code for the rotating knife:
public void HandleRotation()
{
if (transform.rotation.z >= 0.3f)
{
right = false;
}
else if (transform.rotation.z <= -0.3f)
{
right = true;
}
if (right)
{
begin = begin + 0.05f;
}
else
{
begin = begin - 0.05f;
}
var tiltAroundZ = begin * tiltAngle;
var target = Quaternion.Euler (0, 0, tiltAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
Answer by tormentoarmagedoom · Feb 27, 2020 at 03:15 PM
Hello.
First, NEVER THINK THIS :Perhaps it is even impossible to do?
Second, about your problem, you have so many ways to move an object in the scene, basicly you have 2 "families" of function, the ones coming from transform, and the others coming from the Rigidbody (if exists)
I dont know whats the propouse of your knife to move so i dont know whichonce is better for your case.
As you are still "noobie" I recommend you to spend some hours reading/watching manuals/tutorials about this: (Dont try to go fast and find the solution to your only problem. A lot of things you dont even know they exists can be shown to you and give solutions you never imagined. Take your time)
https://docs.unity3d.com/ScriptReference/Transform-position.html https://docs.unity3d.com/ScriptReference/Transform.Translate.html https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html https://answers.unity.com/questions/1004821/what-are-ienumerator-and-coroutine.html https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
Good luck!
Actually it does not matter in my case if it will come from rigidBody or transform. I have attached rigidbody, too. What I care most about is to actually move object to the diagonal direction. I have looked at most of your provided links and have not found any way to move object diagonally, keeping knife end direction.
Your answer
![](https://koobas.hobune.stream/wayback/20220612224038im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate GameObject with Velocity 1 Answer
WHY IK affects Rigidbody???? 0 Answers
How to specify a maximum height velocity for rigidbody game object 0 Answers
Making Hanzo’s Scatter Arrow in 2D 0 Answers