Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[Mounts] Help!

Discussion in 'Scripting' started by UBTech, May 1, 2014.

  1. UBTech

    UBTech

    Joined:
    Dec 30, 2012
    Posts:
    9
    I have a horse model with animations. I want to be able to mount that horse by clicking on it being able to run around as the horse with the character attached. Can someone help? Thank you
     
  2. UBTech

    UBTech

    Joined:
    Dec 30, 2012
    Posts:
    9
    Bump need this ASAP
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    I haven't done this before but I can give some suggestions.

    One, you could make a copy of the horse and its animations that have the character attached to it. Once you've got that you'd just use script to remove the character and normal horse and replace them with the mounted horse and to switch from controlling the character to controlling the mounted horse. (I think World of Warcraft does it this way.)

    Two, you could have an animation for the character that's in a riding position (and also one for mounting / dismounting) and use script to make it stay relative to where the horse is as it moves, again using script to switch from controlling the character to controlling the horse (I think Red Dead Redemption does it this way.)

    Either way you'll need to make new animations. Software such as Motionbuilder is good for doing this, but there are alternatives such as Blender that aren't quite as easy to animate in.
     
  4. UBTech

    UBTech

    Joined:
    Dec 30, 2012
    Posts:
    9
    I do not care for the animations at this point. The character for all I could care if walk up to my horse model (Which already has animations) and the character to be in a standing position for now. It doesn't bother me I will get to the PROPER anims asap. If you could tell me how to do so? It would be excellent. I am sure there is a way for you to walk up to it press E for example and Jump on the horse disabling the Characters functions and enabling the horse to act as the character?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  6. UBTech

    UBTech

    Joined:
    Dec 30, 2012
    Posts:
    9
    I'm so dense I think I might need a step by step tutorial :( FFS -.-
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I've made a script that should work and be a basis. It may need some adjustments and can be improved alot, it's written in ~ 10-15 mins though and should rather represent the basic idea that i would use when i need such a script:

    This is the full class, rename the script you put it into like the class or name the class like your scriptname.

    In order to test this, you'll need at least the following in your scene:

    1) player onject , mount object
    2) player object only needs to have the script attached to it and, for easy testing, a rigidbody ( not using gravity and set to isKinematic) in order to make the triggering work (you can replace it by a CharacterController too and ajust the movement code)
    3) your mount object needs a childObject, which i called 'mountTrigger' ... i used a simple cube and rescaled it so that it covers the area around the mount, disable the renderer and set the collider component to 'isTrigger'
    4) add the tag 'mount' to this childObject

    5) if i haven't forgot to mention anything, this should be working and give you a basic idea


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class playerMountControl : MonoBehaviour {
    6.  
    7.     private bool useMount; // are we in range of the mount?
    8.     //private bool mounted; // this was actually implemented for further control when mounted, not needed at this current state
    9.  
    10.     public Transform mountObject; // the reference for the mount object that we will sit on
    11.     public Transform objControl;     // reference to the transform that our movement input will affect
    12.  
    13.     public Vector3 mountedPos;    // the position the player will be moved to when we use the mount
    14.     public float moveSpeed = 2f;    // player's walking speed
    15.     public float mountSpeed = 3f;   // additional speed through the mount
    16.  
    17.     void Start ()
    18.     {
    19.         useMount = false;               // to be sure it's false in the beginning, let's explicitly tell unity to do so
    20.         //mounted = false;
    21.         objControl = transform;        // in the beginning we want to move our player, thus we put the object's transform this script is attached to
    22.     }
    23.    
    24.    
    25.     void Update ()
    26.     {
    27.         if (useMount  Input.GetKeyDown(KeyCode.E))  // if we entered the mountTrigger and press E
    28.         {
    29.             mountObject.FindChild("mountTrigger").gameObject.SetActive(false); // just to prevent some bugs, disable the trigger temporarily
    30.             useMount = false; // we don't want the GUI to show the message anymore and do not want this code to be able to be executed again while we sit on the mount
    31.             //mounted = true;
    32.  
    33.             transform.parent = mountObject.transform; // parent the player to the mount so that he moves with its transform
    34.             objControl = mountObject; // now we want to control the mountObject, not the player itself anymore
    35.             mountedPos = mountObject.position + Vector3.up; // you can also use a gameObject for the position offset
    36.            //so that you can adjust it in runtime/inspector by moving it instead of editing the script or a public vector variable
    37.             moveSpeed += mountSpeed; // let's add the speed for the movement, don't forget to remove it when unmounting
    38.  
    39.             transform.position = mountedPos; // let's finally put the player object onto the mount
    40.         }
    41.  
    42.         if(Input.GetKey(KeyCode.W))
    43.         {
    44.             objControl.Translate(Vector3.forward*moveSpeed*Time.deltaTime); // just move forward
    45.         }
    46.         if(Input.GetKey(KeyCode.S))
    47.         {
    48.             objControl.Translate(Vector3.back *moveSpeed* Time.deltaTime); // backwards
    49.         }
    50.        
    51.         // more input
    52.         // plus it's better to make a movement vector and first add all the input,
    53.         // transform the direction to worldspace and normalize the vector before moving
    54.     }
    55.  
    56.     void OnGUI()
    57.     {
    58.         if (useMount) // when this is set to true, we want the text to be shown
    59.         {
    60.             GUI.Label(new Rect(Screen.width/2-50, Screen.height/2-10, 100,20), "Press 'E' to use the mount!");
    61.         }
    62.     }
    63.  
    64.     void OnTriggerEnter(Collider trigger)
    65.     {
    66.         // enable the Label showing further instructions and 'enable' the key input for mounting
    67.         // but only do so if the trigger we entered is the trigger of a mount
    68.         if (trigger.tag == "mount")
    69.         {
    70.             useMount = true; // now we enable the piece of code in update, so that only the key needs to be pressed
    71.             mountObject = trigger.transform.parent; // let's already get the mount in our range, which is the parent object of our trigger
    72.         }
    73.     }
    74.  
    75.     void OnTriggerExit(Collider trigger)
    76.     {
    77.         // disable the piece of code for mounting when we exit that trigger
    78.         if (trigger.tag == "mount")
    79.         {
    80.             useMount = false; // disable the code so that we can press E as often as we want when we do not enter a mountTrigger
    81.             mountObject = null; // i don't know why, this may cause some exceptions later
    82.             // but also prevents bugs like attaching us to the wrong object whatever... needs to be checked whether it's null or not whenever you try to do something with the object this is
    83.             // you can remove it if you want to
    84.         }
    85.     }
    86. }
    87.  
     
    Last edited: May 1, 2014
  8. UBTech

    UBTech

    Joined:
    Dec 30, 2012
    Posts:
    9
    Dont suppose you could PM me about this to talk further about that matter?
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Did it work as you expected or is there something that didn't work/ you wanted to have different.
    You can PM me whenever you want to, but i'm not a professional developer.

    However, if you want to ask first and come to forums whenever i cannot help you, you can do this too.
     
  10. DeveloperBean

    DeveloperBean

    Joined:
    Jul 7, 2018
    Posts:
    2
    1. void Update ()
    2. {
    3. if (useMount & Input.GetKeyDown(KeyCode.E)) // if we entered the mountTrigger and press E
    4. {
    Put "&" between useMount and Input, fixes the errors from what may have been a typo.
     
  11. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Haha yes, that was typed in the browser... I hate to look at code that I wrote 5 years ago. Wouldn't recommend to use the code "as is". It's not that good.
     
  12. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,302
    Should be "&&", not "&".
     
  13. DeveloperBean

    DeveloperBean

    Joined:
    Jul 7, 2018
    Posts:
    2
    Single works for me.
     
  14. M1h3

    M1h3

    Joined:
    Dec 26, 2022
    Posts:
    1
    When press "E", my character does not appear on top of my mount while the mount is moving? Only the mount will move around.
     
  15. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,587
    The above script parents the mount to the player, so the player should move when the mount transform moves. Maybe you have a weird setup where you constantly overwrite your player position in worldspace, or you made a mistake. As this is a 10 year old thread in which not even the OP bothered to reply anymore, i would suggest just making a new thread.