Forum rules - please read before posting.

Double click to teleport

Hi,

I bought AC a few days ago. I'm still trying to wrap my head around it. No need for a complex answer just hope you could point me in the right direction here.

I'm trying to replace the double click running with a teleporting.

I've put a script on the player and add an double click event. And i've also found a way to teleport the player.

Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
KickStarter.player.transform.position = new Vector3(pos.x, pos.y, 0);

I want to run this code only:
1. If the point is inside of the NavMesh
2. If mouse is not hovering a hotspot

And finally i want to align the character in the right direction. I've found this line of code:

KickStarter.player.SetLookDirection();

And i suppose the character will then face the point? Can i rather set the direction itself? Up / Down or 0 / 180 in degrees.

Thanks for help :)

Comments

  • The PlayerMovement script's ClickPoint function can be made use of here, to replace your own ScreenToWorldPoint call, and also only work when over a NavMesh.  To re-position the Player, it's also best to use the provided Teleport function:

    Vector3 worldSpaceNavPoint = KickStarter.playerMovement.ClickPoint (KickStarter.playerInput.GetMousePosition (), true);
    if (worldSpaceNavPoint != Vector3.zero)
    {
        KickStarter.player.Teleport (worldSpaceNavPoint);
    }

    You should also make sure, to avoid conflict, that the Settings Manager's Double-click movement field is set to Disabled.

    About the character's direction: it's not clear what the intented rotation should be, but you can set it with SetRotation instead.

    For a full rundown of all the public functions available when scripting, see the Scripting Guide.
  • Thanks,
    by the angle i mean that the character should be aligned to direction that was teleported. From left to right = align to the right.

    The SetRotation function is exactly what i need except why is the angle shifted?

    This is the angle code:

    Vector3 charPos = KickStarter.player.transform.position;
    Vector3 mousePos = KickStarter.playerMovement.ClickPoint(KickStarter.playerInput.GetMousePosition(), true);

    float angle = Mathf.Atan2(mousePos.y - charPos.y, mousePos.x - charPos.x) * Mathf.Rad2Deg;
    angle = angle < 0 ? 360f + angle : angle;

    Any idea how to simply shift the angle?

    Teleport:
    If a normal click is used outside of the NavMesh the character still goes to a new position within the NavMesh. Can i somehow use this destination point for the doubclick as well?

    I'm trying to work with the scripting guide. There's so many things tho, it's kinda hard to orientate in that. But i hope that's just a matter of time.
  • I don't know what you mean by the angle being shifted, but there's no need for trigonometry:

    Quaternion newRotation = Quaternion.LookRotation (charPos - mousePos);

    The SetRotation method accepts a Quaternion as an alternative to an angle.

    • If a normal click is used outside of the NavMesh the character still goes to a new position within the NavMesh. Can i somehow use this destination point for the doubclick as well?
    You previously said that you wanted it to only work when double-clicking the NavMesh, so there was no need for that originally. 

    AC's "clicking outside NavMesh" behaviour works by shifting the queried screen-position downward until something is found.  The code for it is in PlayerMovement's PointControlPlayer function, but it's not accessible like ClickPoint is.

    You can still use the same principle in your own script, though:

    http://pasteall.org/991076/csharp

  • edited June 2018
    Aye, i didn't want to get outside of the NavMesh with the character itself. I didn't know that clicking only over the NavMesh would feel limited at that point.

     
    Shifted by 90°. Those are the angles the script i've posted returns. Based on that i thought that right direction is 0° by default.

    Thanks for the info. Very appreciated.

    Edit: So I've managed to get the correct angle. Character gets teleported and aligned to the correct direction. But after that it gets overwriten and the character turn again by some built in system.

    ( Character in the back of the screen facing UP - teleported to the foreground and aligned to face DOWN - and after that turns left - or somewhere )

    Can i somehow turn off this turning feature?

    Video example:
    https://youtu.be/327p4lK7KSg
  • What's your current movement method?  It may be that you also have to halt the player's previous movement commands:

    KickStarter.player.Halt ();

    If that makes no difference, you'll have to post the actual code you're using - it'd just be guesswork without seeing it.
  • edited June 2018
    So it turned out that the unwanted turnig was caused due to the angle value put in the SetRotation() function. I've rewrited this part and it now works as intended.

    Inside the NavMesh, teleporting not directly (first attemp) but on some later attempt causes the character to teleport and then walk away somewhere. I've put the Halt() function you mentioned inside without resolving the issue.


  • edited June 2018
    Again, what's your current movement method?  Does it occur every time after the first?

    Where exactly is he moving to?  Enable Gizmos at the top of the Game window and select your Player in the Hierarchy.  Is he pathfinding (i.e. walking along a blue line)?

    The SetRotation method also accepts the more reliable Quaternion - which I demonstrated earlier:

    Quaternion newRotation = Quaternion.LookRotation (charPos - portPos);
    KickStarter.player.SetRotation (newRotation);

    Please use http://pasteall.org/ when posting code, as opposed to using screenshots, so that I can test it accurately.
  • edited June 2018
    The ovement method is point & click. 2D game with polygon collider.

    I didn't use the Quaternion because it doesn't return the angle i want. On the y-axis it  makes the character turn to left or right. I suppose i don't undestand the value it returns. I want to be able to align to all directions based on the angle between the two points. ( As in the video below )

    Code from the screenshot: http://pasteall.org/992542

    Based on the Gizmos mode i now have my guess what is happening there.

    It seems to be overwritten by the code in the PlayerMovement's PointControlPlayer function you mentioned before. Because the character walks exactly to the position where the character is sent if i click (normal click) outside of the NavMesh.

    Only the final point from the PointControlPlayer function is different than the one from the screenshot / pasteall for the double click.
    The Halt function doesn't stop the character from walking in the script though.

    Video: https://youtu.be/sNIu-rEa6_M
  • Strangely enough I can't recreate the problem, but since you're checking for double-clicks in OnGUI it could be because regular AC input is detected in Update.

    Try this instead:

    It uses Update (and AC's own double-click check) to perform the teleport, and throws in a double of click-resets just in case (again, I can't recreate - so I'm just being extra-safe).  If still no luck, try affecting your script's Script Execution Order so that it occurs before AC's StateHandler.
  • Thanks, reseting the clicks and ending the path did the trick :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.