This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

[Godot 3.0 RC1 ] Camera Around Object with Swipe

raphale2767raphale2767 Posts: 2Member
edited January 2018 in 3D

As I can rotate the camera around an object with the swipe, I currently use the following function:

func set_position():
    position.x =  (distance * cos(angle)) - (distance * sin(angle))
    position.y =  position.y
    position.z =  (distance * sin(angle)) + (distance * cos(angle))
    self.set_translation(position)
    look_at_from_position(position, Vector3(0,0,0), Vector3(0,1,0) )

this code work fine with the keyboard

func _process(delta):
    if  Input.is_action_pressed("ui_left") :
        angle -= 0.01
    if  Input.is_action_pressed("ui_right") :
        angle += 0.01
    if  Input.is_action_pressed("ui_down") :
        distance -= 0.05
    if  Input.is_action_pressed("ui_up") :
        distance += 0.05
    set_position()

But I can't generate a fluid swipe:

FULL SWIPE CODE

extends Camera

signal swipe(direction)

var swipe_start   = null
var minimum_drag  = 100
var startedToDrag = false

var distance     = 12
var position     = Vector3(0, 9 , distance)
var angle        = 0

func _ready():
    set_process(true)
    connect("swipe",  self, "_swiped")
    pass


func _process(delta):
    if  Input.is_action_pressed("ui_left") :
        angle -= 0.01
    if  Input.is_action_pressed("ui_right") :
        angle += 0.01
    if  Input.is_action_pressed("ui_down") :
        distance -= 0.05
    if  Input.is_action_pressed("ui_up") :
        distance += 0.05
    set_position()

func set_position():
    position.x =  (distance * cos(angle)) - (distance * sin(angle))
    position.y =  position.y
    position.z =  (distance * sin(angle)) + (distance * cos(angle))
    self.set_translation(position)
    look_at_from_position(position, Vector3(0,0,0), Vector3(0,1,0) )

func _swiped(direction):
    if direction == "left":
        angle -= 0.01
    if direction == "right":
        angle += 0.01
    if direction == "down":
        distance -= 0.005
    if direction == "up":
        distance += 0.005

func _unhandled_input(event):
    if event.is_action_pressed("click"):
            startedToDrag = true
            swipe_start   = get_viewport().get_mouse_position()

    if startedToDrag :
        _calculate_swipe( get_viewport().get_mouse_position() )

    if event.is_action_released("click"):
        startedToDrag = false
        _calculate_swipe( get_viewport().get_mouse_position() )

func _calculate_swipe(swipe_end):
    if swipe_start == null: 
        return
    var swipe = swipe_end - swipe_start
    if abs(swipe.x) > minimum_drag:
        if swipe.x > 0:
            emit_signal("swipe", "right")
        else:
            emit_signal("swipe", "left")

Tags :

Answers

  • vnenvnen Posts: 31Member Godot Developer

    Gosh, pleas format your code properly:

    Surround the code with three backticks:

    ```
    func code():
    return test
    ```

    Result:

    func code():
        return test
    
  • raphale2767raphale2767 Posts: 2Member

    @vnen said:
    Gosh, pleas format your code properly:

    Surround the code with three backticks:

    ```
    func code():
    return test
    ```

    Result:

    func code():
      return test
    

    thank you I did not know how to do that.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.