Question by
Ronin-Projekt · Oct 12, 2016 at 09:41 PM ·
c#rigidbodymodeldirectionrotate object
How to change the direction of the model?
Hi. How to change the direction of the model?
How to rotate the model during the change of direction?
I'm looking suggestions or solutions.
I tested several solutions. But without success.
This is not a model of the sphere.
Any suggestions.
Test model.
Download *.fbx https://www.dropbox.com/s/dlkl7tn8whgo2kl/testDron.fbx?dl=0
public float Acceleration = 100f;
public float MaxSpeed = 1.5f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//Store the current horizontal input in the float moveHorizontal.
float moveHorizontal = Input.GetAxis("Horizontal");
//Store the current vertical input in the float moveVertical.
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); // (x, y, z)
if (rb.velocity.magnitude < MaxSpeed)
movement = movement.normalized * Acceleration * Time.deltaTime;
rb.AddForce(movement);
}
dron.jpg
(217.5 kB)
Comment
Answer by Ronin-Projekt · Oct 15, 2016 at 08:31 AM
One step closer. New code and video.
https://www.youtube.com/watch?v=jyGIuVA5yAQ&feature=youtu.be
public float Acceleration = 99f;
public float MaxSpeed = 1.5f;
private Rigidbody rb;
public float turnSmoothing = 15f; // A smoothing value for turning the player.
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate() {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 targetDirection = new Vector3(moveHorizontal, 0.0f, moveVertical); // (x, y, z)
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp(GetComponent<Rigidbody>().rotation, targetRotation, turnSmoothing * Time.deltaTime);
if (moveHorizontal != 0f || moveVertical != 0f)
{
GetComponent<Rigidbody>().MoveRotation(newRotation);
rb.AddForce(moveHorizontal,0, moveVertical);
}
// if (rb.velocity.magnitude < MaxSpeed)
// targetDirection = targetDirection.normalized * Acceleration * Time.deltaTime;
// rb.AddForce(newRotation);
}