I need example for script (3D, 4 legged character)

Hello guys!
My child is doing school project in Unity and she asked me to help her with this one. She created landscape and imported prefabricated free horse from Unity store (yes, it’s project which includes horse riding, but without human, horse is character). She imported this horse: Unity Asset Store - The Best Assets for Game Making , but as I can see, it doesn’t come with scripts for controlling and changing animation regarding pressed key (idle, walk, run). We both are struggling with First Person and Third Person Controller but not satisfied with results. Can someone help us with some basic control script which includes moving (forward, backward, left, right and jump) and how to activate different animation when other key is pressed.
Thanks in advance.

Quadrupeds, as four-legged characters are called (might help with searching for future problems), don’t have any support in the standard assets for all i know. Doing a quick search, I didn’t find any useful ready-to-go packages either.

It seems that you need to either settle with poor collision geometry by using the biped solutions or bite the bullet and code your own.

The latter would probably require a rigidbody, a mesh collider or compound collider, and at least one script for the movement.

Edit: Oh, I just saw you were already going for a script in the question’s title… let me write somewthing quick:

class QuadrupedMovement : MonoBehaviour
  private Rigidbody rb;
  public float speed = 5;
  public float rotateSpeed = 5;

  private void Awake()
    rb = GetComponent<Rigidbody>();

  private void FixedUpdate()
    var input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    input = Vector3.ClampMagnitude(input, 1);

    var direction = transform.forward;
    direction.y = 0; // just in case
    direction = Quaternion.Euler(0, Input.GetAxis("Mouse X") * rotateSpeed , 0) * direction;
    var newRotation = Quaternion.Euler(direction);

    rb.velocity = transform.TransformDirection(input) * speed;

I haven’t tested this code yet, but I guess it conveys the general idea. This is, of course, just one of many ways to attempt this. Here, mouse movement rotates the character while WASD / Arrow Keys move forwards, backwards, and strafe.

For the setup: this article has an example for a possible mesh collider:
Mesh Collider
A horizontal CapsuleCollider might work as well.

Make sure to freeze X and Z axes with the rigidbody’s rotation constraints.
For smooth rotation, slap this script of mine onto the object as well and leave the rigidbody’s Interpolation setting on “None”. Mind the scripts comment by the way.

Hope this helps.


Thanks for the script! However, there’s an error in code:

direction *= Quaternion.Euler(0, Input.GetAxis(“Mouse X”) * rotateSpeed , 0);

It says: error CS0019: Operator *=' cannot be applied to operands of type UnityEngine.Vector3’ and `UnityEngine.Quaternion’