Godot Version
4.2.1.stable
Question
When i press “E” near to the car, im getting into the car without problems, but when im already in the car and press “E”, im spawning many meter below the car.
The code of the character:
The code of the car:
4.2.1.stable
When i press “E” near to the car, im getting into the car without problems, but when im already in the car and press “E”, im spawning many meter below the car.
The code of the character:
The code of the car:
When the player exits where do you set their new position? Am I missing that piece of code?
The player exits on the same x and z position but it clips through the ground the car was on so maybe -100 on the y position
No, what I am trying to ask is where do you update the players position relative to the vehicle?
I see when you enter you set the players positio to == the position of the vehicles camera, but I don’t see where you update the players position when exiting. Also, is there a reason you set the players position to the position of the camera rather than the vehicle?
Its exiting at the same pos cuz its at the same pos when driving and the players.pos is = car.pos he cant move when driving, only the car moves. And when his not driving anymore its not the case anymore, than he can move but not the car
Okay, where is that line of code? Player.pos = car.pos?
I guess I am missing an important piece of your code that I am not seeing. I don’t see where you are attaching the player position to the car position. If it is working in your game, then it is there, I am just not seeing it in your code here.
line 75-77
I realized that as I was rewritting your code to understand it better, that that is being called if inside.
Read through this code and see if it looks like it’ll do what you want it to, I put some comments because I wasn’t sure why you had certain things set up the way you did, and other things I didn’t bother retyping since it wasn’t concerning your question.
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
@onready var Cam1Piv = %CamOrigin
@onready var sens = 0.5
@onready var inside = false
@onready var Cam2 = $"../VehicleBody3D/Camera3D2"
@onready var Cam1 = $CamOrigin/SpringArm3D/Camera3D
@onready var inside_car = false
@onready var animateSprite := get_node("AnimatedSprite3D")
@onready var char_may_move = true
signal sig
var gravity : float = ProjectSettings.get_setting("physics/2d/default_gravity")
func _ready() -> void:
# You've already made the other two var false in your variable
Cam1.current = true
Cam2.current = false
func _input(event: InputEvent) -> void:
# This doesn't seem to concern your question so left it alone
# This is the part I moved from physics
if Input.is_action_just_pressed("E") and inside:
vehicle_interaction()
func _physics_process(delta: float) -> void:
if Input.is_action_pressed("forward"):
#Changed this since you have a var, might as well use it
animateSprite.play("walking")
#I don't see an issue with gravity, so I left it alone
#Same with jumping, and the rest of your basic movement
#these could also be changed at the moment you change the variable
#in your body_entered signals
if inside:
$"../CanvasLayer".visible = true
else:
$"../CanvasLayer".visible = false
#I moved this to the _input function,
#why does this input need to be checked 60 times a second?
func vehicle_interaction() -> void:
global_position = Cam2.global_position
#I am uncertain why you set all these up individually,
#the only difference is you add 0 to the y...
#what does adding 0 do to this position?
if inside_car:
Cam1.current = true
Cam2.current = false
char_may_move = true
inside_car = false
animateSprite.visible = true
#Did you want this to be reenabled when exiting?
#I left it as you had it written
$CollisionShape3D.disabled = true
await get_tree().create_timer(1.0).timeout
else:
Cam1.current = false
Cam2.current = true
char_may_move = false
inside_car = true
animateSprite.visible = false
$CollisionShape3D.disabled = true
sig.emit()
await get_tree().create_timer(1.0).timeout
#Why do you set this variable twice?
#inside_car = true
So, I streamlined your code a bit, I am not sure without recreating your game thus far and testing where the point of failure is exactly, however I would be tempted to think it was with the way you had your input heirarchy.
is the vehicle func for the vehicle script
I was rereading what you had, and I think what I wrote won’t work exactly.
If I understand when you are outside the car, but within its area it registers collision and so you set inside to true, however, once you enter car it deactivates collision and registers exiting area and sets it to false? Is this how that works.
If that is how it is set up, you’ll have to tweak the code a little, since it won’t detect the input once you are in the car. Without changing to much you could just do this:
if Input.is_action_just_pressed("E"):
if inside or inside_car:
vehicle_interaction()
no, the vehicle func just replaces all the stuff you had written in the physics_process that handled your vehicle_interactions.
So, I moved the input to enter/exit the vehicle to the players _input function.
I took all the script in your physics function that related to entering/exiting the vehicle and moved it to a function called vehicle_interaction(), that is where you handle entering/exiting vehicle. I call this function from your _input function.
So, for clarity, I deleted everything you have written in your physics_process starting with the if Input.is_action_just_pressed(“E”) and inside: and removed everything to the end of the function. I consolidated it all into an input call in your _input(event) and the new func vehicle_interaction().
but i think i didnt explain the variables
cam1 is for player
cam2 is for car
char_may_move is if the character is allowed to move
sig is when the player enters the car
I actually found it myself. I only had to delete line 80. In case u care if u can use it, I obviously dont mind
Sounds good, I would still recommends maybe streamlining your code a little bit. You don’t really need the code you have for entering and exiting the vehicle to be in physics_process, that is just extra checks your player is making that are unecessary whenever you aren’t even next to a vehicle.
What I have written should work as well, though you can tweak it to your own liking. I would just recommend handling entering/exiting vehicle using _input() as a catalyst rather than physics.
Ik I still have some issues but it works way better than then and thx for the help
Also, if you are going to keep your current code set up delete line 91 aswell. It is redunadant, you can put the code in it under the if input statement above it, since you know inside_car will be false.