+1 vote

Please be helpful my code is a add_force 'd rigid body. my camera doesn't rotate around it

func _input(event):
    if event is InputEventMouseMotion and event.button_mask & 1:
        # modify accumulated mouse rotation
        rot_x += event.relative.x * .0001
#       rot_y += event.relative.y * .01 * PI
        transform.basis = Basis() # reset rotation
        rotate_object_local(Vector3(0, 1, 0), rot_x) # first rotate in Y
#       get_parent().rotate_object_local(Vector3(1, 0, 0), rot_y) # then rotate in X

I Have made the hyrarchy as follows:
-Rigidbody
--3dposition
---camera

camera script (setastoplevel(true) is set to true because my cube which is a 3D rigid body can rotate around, but my camera not):

extends Camera

onready var t1 = get_parent().get_node("RigidBall") #just a meshinstance

func _ready():
    set_as_toplevel(true)


func _process(delta):

#   get_parent().rotate(Vector3(0,0,-10).normalized(), .01)
    pass

var rot_x = 0
func _physics_process(delta):
    rot_x *= .001
    rotate_object_local(Vector3(0, 1, 0), .01)
#   get_parent().rotation_degrees.y += 10
#   transform.basis = Basis()
#   look_at(get_parent().transform.origin, Vector3(0,0,1))
#   get_parent().transform.origin = Basis()
#   get_parent().transform.basis = Basis()
    pass
##  get_parent().get_node("RigidBall").global_transform.basis.x * -1
##  rotate_object_local(Vector3(0,1,0), deg2rad(10))
##  rot_x += 30
##  transform.basis = Basis()
##  rotate_object_local(Vector3(0, 1, 0), rot_x)
#
#
##  get_parent().get_node("RigidBall").transform.origin.y
##  rotate_y(deg2rad(1))
#   rot_x += .01
#   transform.basis = Basis()
##  rotate_object_local(Vector3(0, 1, 0), rot_x)
#   rotate_object_local(Vector3(0, 1, 0),rot_x)


##var rot_x = 0
#var rot_y = 0
#
#func _input(event):
#   if event is InputEventMouseMotion and event.button_mask & 1:
#       # modify accumulated mouse rotation
#       rot_x += event.relative.x * .01 * PI
#       rot_y += event.relative.y * .01 * PI
#       transform.basis = Basis() # reset rotation
#       get_parent().rotate_object_local(Vector3(0, 1, 0), rot_x) # first rotate in Y
#       get_parent().rotate_object_local(Vector3(1, 0, 0), rot_y) # then rotate in X
in Engine by (193 points)

Lookatfromposition() also is problem

1 Answer

0 votes

Have found an asnwer thanks to kidscankode

http://kidscancode.org/godot_recipes/3d/camera_gimbal/

extends Spatial

export (NodePath) var target

export (float, 0.0, 2.0) var rotation_speed = PI/2

# mouse properties
export (bool) var mouse_control = false
export (float, 0.001, 0.1) var mouse_sensitivity = 0.005
export (bool) var invert_y = false
export (bool) var invert_x = false

# zoom settings
export (float) var max_zoom = 3.0
export (float) var min_zoom = 0.4
export (float, 0.05, 1.0) var zoom_speed = 0.09

var zoom = 1.5

func _unhandled_input(event):
    if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
        return
    if event.is_action_pressed("cam_zoom_in"):
        zoom -= zoom_speed
    if event.is_action_pressed("cam_zoom_out"):
        zoom += zoom_speed
    zoom = clamp(zoom, min_zoom, max_zoom)
    if mouse_control and event is InputEventMouseMotion:
        if event.relative.x != 0:
            var dir = 1 if invert_x else -1
            rotate_object_local(Vector3.UP, dir * event.relative.x * mouse_sensitivity)
        if event.relative.y != 0:
            var dir = 1 if invert_y else -1
            var y_rotation = clamp(event.relative.y, -30, 30)
            $InnerGimbal.rotate_object_local(Vector3.RIGHT, dir * y_rotation * mouse_sensitivity)

func get_input_keyboard(delta):
    # Rotate outer gimbal around y axis
    var y_rotation = 0
    if Input.is_action_pressed("cam_right"):
        y_rotation += 1
    if Input.is_action_pressed("cam_left"):
        y_rotation += -1
    rotate_object_local(Vector3.UP, y_rotation * rotation_speed * delta)
    # Rotate inner gimbal around local x axis
    var x_rotation = 0
    if Input.is_action_pressed("cam_up"):
        x_rotation += -1
    if Input.is_action_pressed("cam_down"):
        x_rotation += 1
    x_rotation = -x_rotation if invert_y else x_rotation
    $InnerGimbal.rotate_object_local(Vector3.RIGHT, x_rotation * rotation_speed * delta)

func _process(delta):
    if !mouse_control:
        get_input_keyboard(delta)
    $InnerGimbal.rotation.x = clamp($InnerGimbal.rotation.x, -1.4, -0.01)
    scale = lerp(scale, Vector3.ONE * zoom, zoom_speed)
    if target:
        global_transform.origin = get_node(target).global_transform.origin
by (193 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories