- Home /
Rotating an object according to the force.
I am working on a project now which is about moving a paperboat forth and back. I have applied force to the object which will let the object move just by clicking on a plane. I want the object to spin according to the force too. The object is not rotating at all. Please help me in fixing this problem. My code is...
var strength : float = 10.0f;
var amount: float = 0.0f;
var smooth = 2.0;
var tiltAngle = 30.0;
function Update ()
{ if(Input.GetKeyDown(KeyCode.Mouse0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit))
{
var delta = transform.position - hit.point ;
rigidbody.AddForce(delta.normalized * strength, ForceMode.Impulse);
rigidbody.AddTorque (0, 10, 0);
}
}
}
Please respond to this question as soon as possible. Thank you...
Answer by tool55 · Mar 08, 2011 at 08:35 PM
Forces using the physics engine need to be added inside FixedUpdate() instead of Update() Try that and see if it works.
Here's a link to a post about how to use slerp to look at a target point.
Here's a link to an answer on this forum that deals with limiting slerp rotation to one axis. See if this helps.
http://answers.unity3d.com/questions/36315/rotate-on-one-axis
Thanks a lot for your opinion about this. As you said, i have tried doing FixedUpdate() for this. But, it didn't work :( The camera is on Y axis, it looks downwards.. Plane is on the origin. Boat is on top of the plane. I want the boat to spin with respect to the torque. Its not working now either :( Please give me some other ideas.. or help me out with a new proper code for the boat to spin.. Thanks in advance..
P.S. with the current code, the boat keeps spinning in the same angle for a while and after some time, it stops spinning even.:(
I think the problem is that you're only adding torque for the brief moment when the mouse button is down. Are you trying to get the boat to look at the hit.point of the raycast? If so, you might be better off using slerp for your rotation. I'll update my answer above to include a link to a similar post.
Thanks a lot for your help dude. I will never forget this help from you. Please keep helping me in my forthco$$anonymous$$g doubts too. and an important request. I am a bit weak in the basic program$$anonymous$$g. Can you please guide me in how I can improve my program$$anonymous$$g ability ? Like.. Guiding me in.. what kind of books i can study and all that.. Thanks in advance again :) U made me smile.. Thanks a lot again..
I only started with Unity about a year and a half ago, maybe two years, with no program$$anonymous$$g experience at all. I'm still astounded by the knowledge of some of the people on these forums. I bought a basic book on Javascript and read it cover to cover, then gave myself challenges in Unity ($$anonymous$$i games to make) and posted dozens of questions. And I visit the scripting forum daily looking for one new piece of info that I didn't know before. I would also recommend getting Will Goldstone's Unity Essentials book and reading it through. Will is a great $$anonymous$$cher. His website is www.unity3dstudent.com
Your answer
Follow this Question
Related Questions
Add torque is not behaving same in opposite Y direction. Need help! 1 Answer
Stop rotation on collision 1 Answer
How to remove all force from an object. 2 Answers
realizing AddTorqueAtPosition - how to calculate forces 0 Answers
Returning a rigidbody back to its original x and z rotations through physics forces. 2 Answers