How do I use normalized correctly! 3D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dragon20C

I want to use normalized but with different speeds for the side movement I saw a tut where you times the vector3 in “move and slide” but that only provides one type of movement instead of the sides being slower and front and back being faster, also has an issue where the players speed isnt getting applied and it moves very slowly when I use movement = movement.normalized.

here is the code:Legobet88 🀄 Trustworthy Agen Situs Slot Online Bet88 Terpercaya Ter- Gacor Hari Ini 2024

Thanks for posting your code. I don’t have a place to run it right now, but here are a couple of observations

  1. When you used .normalized() it scales the vector to have a length of 1 in any direction. In your code, you are setting your movement vector based on your movement_speed_* variables, but then you apply .normalized after that which will set the vector to the same length in any direction. In your case, I do not think you need to used normalized - you would only need that if you want to convert a vector with varying length to a direction. In your case you want a vector with varying length depending on the speed in each direction.

  2. ​in _physics_process(delta) you are calling _input(delta). The variable delta is the time since the last frame, but _input expects an input event object, so that is not going to work. Godot will automatically call your _input function with events when they happen so you do not need to call it yourself from _physics_process

AndyCampbell | 2020-11-27 20:06

I got told normalized stops the player from going faster when two keys are pressed e.g (a,d) so I would like to implement normalized so it stops the play from gaining speed/going faster sideways how would I stop that without normalized.

Dragon20C | 2020-11-27 21:02

Ok, now I understand the problem…

Yes, you are right - assuming you had equal movement in all directions (your example does not) - for example right = (1,0) and down = (0,1) - selecting both together means you move (1,1) on a diagonal which has length = 1.41. So, the player moves further when 2 keys pressed. Normalize will stop that by setting the length of the diagonal vector to 1, so that makes your right (0.71,0) and down (0,0.71).

In your case, you want a movement of 15 for forward, or 8 for other directions but normalize will set the speed in all directions to 1 (or 0.71 if moving diagonally), so this will cancel the effect of your movement_speed_*

You could move the speed calculation after the normalize.

To account for the faster speed when moving forward, you would need to increase that move when you scale
Maybe something like this (I have not tested this in 3d, so it might have errors)

var movement = Vector3()
var movement_speed = 8  
var forward = false

var mouse_sen = 0.3

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
​
func _physics_process(delta):
	if Input.is_action_pressed("ui_cancel"):
		get_tree().quit()

	var direction = Vector3()

	if Input.is_action_pressed("ui_up"):
		direction += -transform.basis.z 
		forward = true   # temporary flag to track that movement includes a forward element
	else:
		forward = false

	if Input.is_action_pressed("ui_down"):
		direction += transform.basis.z 
	if Input.is_action_pressed("ui_left"):
		direction += -transform.basis.x 
	if Input.is_action_pressed("ui_right"):
		direction += transform.basis.x 
	
	direction = direction.normalized() # this will always be length of 1 but you need it longer when moving forward
	if forward:
		direction += -transform.basis.z   # Double speed if moving forward, after normalizing
	movement = direction * movement_speed
	movement = move_and_slide(movement,Vector3.UP)

AndyCampbell | 2020-11-28 01:12

1 Like