- Home /
 
 
               Question by 
               programad · May 01, 2014 at 12:45 AM · 
                physicsairacingartificial intelligence  
              
 
              Turn character by force
I am moving the character by force using this:
 var vertical = Input.GetAxisRaw ("Vertical");
 var forwardMoveAmount = vertical * moveSpeed * 100;
 
 if (vertical > 0.0f) {
     rigidbody.AddRelativeForce (0, 0, forwardMoveAmount);
 } else if (vertical < 0.0f) {
     rigidbody.AddRelativeForce (0, 0, forwardMoveAmount);
 } else {
     if (rigidbody.velocity.z != 0) {
         rigidbody.AddRelativeForce (0, 0, -forwardMoveAmount * moveSpeed * Time.deltaTime * 2);
     }
 }
 
               And turning the character like this:
 float h = Input.GetAxisRaw ("Horizontal");
 
 Vector3 a = transform.localEulerAngles;
 
 // Turns the character to the forward direction
 if (h > 0.1)
     transform.localEulerAngles = new Vector3 (a.x, a.y + (turnSpeed * Time.deltaTime * 20), a.z);
 else if (h < 0)
     transform.localEulerAngles = new Vector3 (a.x, a.y - (turnSpeed * Time.deltaTime * 20), a.z);
 
               This works fine to player, but I'm trying to develop an AI and I can't figure out how to smoothly change the direction of the AI to the new point in path. For now I am setting the new waypoint and just pointing the AI to there using LookAt. How can I "smooth" or damp the curve to a more natural behaviour?
               Comment
              
 
               
              Answer by getyour411 · May 01, 2014 at 01:18 AM
Here's what I'm doing, hope it works/answers (although this is not via Force)
Define/set rotationspeed, myTransform is cached reference to Enemy self (transform)
     private void controlledLookAtTarget ()
     {
             Vector3 lookDir = (myTarget.position - myTransform.position);
             lookDir.y = 0f;
             myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation (lookDir), rotationSpeed * Time.deltaTime);
     }
 
              Your answer