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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

First Person Lance Movement?

Discussion in 'Scripting' started by Adam_Raetz, Oct 14, 2015.

  1. Adam_Raetz

    Adam_Raetz

    Joined:
    Sep 22, 2015
    Posts:
    6
    I am making a jousting "simulator".

    Basically I am trying to figure out how to script the movement of lance. I'm not worried about any form of lunges or torso twisting by the player, just moving the lance in a manner which allows for the player to need skill to hit the practice targets.

    lanceNeed.jpg


    I understand that the movement needs to be in a cone, so I need to just manipulate the tip of the lance. The player would be holding the lance in the lower right of the screen.

    I've played around with trying to use some the vanilla scripts for key input (my preferred form of input for this task as mouse look has a different function) and then piecing together some custom scripts, but in every case the lance jets away from my player object. I cannot figure out how to anchor it to the player object (yes I have it as a child to the player).

    Thanks for any help, I'm pretty stumped and can't seem to find anything in a tutorial or the API that really jumps at me as a solution.
     
  2. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    I would try to set the pivot point to the point where the player is holding the lance. And translate x/y mouse movements to clamped rotation on the lance, so that 0x/0y mouse position (middle of screen) would translate to the lance pointing straight forward, and +x would translate to the lance pointing left and so on.

    Edit:

    Sorry I re-read and saw that you preferred key inputs. Well then, you could maybe have

    Code (CSharp):
    1.  
    2. GameObject lance;
    3.  
    4. float h = 0.0f;
    5. float v = 0.0f;
    6.  
    7. void Update(){
    8.     If(Input.GetKeyDown(KeyCode.W)){
    9.         v += somefloatvalue * Time.deltaTime;
    10.     }
    11.    
    12. If(Input.GetKeyDown(KeyCode.S)){
    13.         v -= somefloatvalue * Time.deltaTime;
    14.     }
    15.  
    16.     h = Mathf.Clamp(h, minfloat, maxfloat);
    17.     v = Mathf.Clamp(v, minfloat, maxfloat);
    18.  
    19.    lance.transform.Rotate(Quaternion.Euler(new Vector3(h, v, 0));
    20. }
    Obviously not completed script but you get the idea, hope that helps :D
     
    Last edited: Oct 14, 2015
  3. Adam_Raetz

    Adam_Raetz

    Joined:
    Sep 22, 2015
    Posts:
    6
    Thanks much that pointed me in the right direction