[Godot 3.0 RC1 ] Camera Around Object with Swipe

raphale2767raphale2767 Posts: 2Member
edited January 15 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")
Tagged:

Tags :

Answers

  • vnenvnen Posts: 31Admin 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.

Sign In or Register to comment.