- Home /
AddTorque is not working.
using UnityEngine;
using System.Collections;
public class CONTROL : MonoBehaviour
{
public float amount = 10f;
void Update()
{
float v = Input.GetAxis("Vertical")*amount*Time.deltaTime;
float h = Input.GetAxis("Horizontal")*amount*Time.deltaTime;
rigidbody.AddTorque(transform.right*v);
rigidbody.AddTorque(transform.forward*h);
}
}
This is my script for applying torque to a cube. when I run this and press a key it does nothing but show me this message:
"transform.position assign attempt for 'Cube' is not valid. Input position is { NaN, NaN, NaN }."
This is my second question, thanks for all the answers I have received but none have worked yet.
when I run this and press a key it does nothing but show me this message
What key are you pressing and where are you handling that? If you have another script that handles keypress, you need to post that too.
No, all input is handled in this part of the script:
float v = Input.GetAxis("Vertical")*amount*Time.deltaTime;
float h = Input.GetAxis("Horizontal")*amount*Time.deltaTime;
the keys I press are the arrow keys or the "w s a d" keys that make up the vertical and horizontal axis.
Answer by $$anonymous$$ · Sep 09, 2013 at 05:03 PM
This seems to work for me.
using UnityEngine;
using System.Collections;
public class Control: MonoBehaviour
{
public float amount = 10f;
private float v, h;
void Start()
{
if(rigidbody == null)
{
gameObject.AddComponent<Rigidbody>();
rigidbody.useGravity = false;
}
}
void Update()
{
v = Input.GetAxis("Vertical")*amount*Time.deltaTime;
h = Input.GetAxis("Horizontal")*amount*Time.deltaTime;
}
void FixedUpdate()
{
rigidbody.AddTorque(transform.right*v);
rigidbody.AddTorque(transform.forward*h);
}
}
On a side note, it's good to handle Physics stuff inside FixedUpdate if you can.
Answer by Rakibulalam · Jan 28, 2016 at 01:23 PM
GetComponent<Rigidbody>().AddTorque(transform.forward* h, ForceMode.VelocityChange);
GetComponent<Rigidbody>().AddTorque(transform.right* v, ForceMode.VelocityChange);
Add force mode and make sure your angularDrag is zero.
Your answer
Follow this Question
Related Questions
rigidbody.AddRelativeTorque(), help 1 Answer
Using Torque to kill Angular Velocity 2 Answers
Keep current force applied based on Vector3.forward? 1 Answer
AddTorque used properly? 0 Answers
AddTorque Isn't Working? 1 Answer