Auto Move character to specific target instead of keyboard controls Unity3d
up vote
0
down vote
favorite
I want to Auto Move my character to following object, instead of keyboards controls, my code works fine on keyboards controls, I am trying to add a target as a gameobject and trying to character to follow it, "dummy" is variable is my public var, where I assign my target as a gameobject", here is my code so far,
protected virtual void UpdateMovement()
{
// var local = Input.GetAxis("Horizontal") * Vector3.right + Input.GetAxis("Vertical") * Vector3.forward;
//works fine at keyboards controls
var local = new Vector3(0,0,0);
local = dummy.transform.position - transform.position; // I tried this but did'nt work
transform.LookAt(dummy.transform);
var movement = new CharacterMovement();
movement.Direction = getMovementDirection(local);
if (WalkWhenZooming && _controller.ZoomInput)
{
movement.Magnitude = 0.5f;
movement.IsSlowedDown = true;
}
else
{
if ((_motor.ActiveWeapon.Gun != null || _motor.ActiveWeapon.HasMelee) && FastMovement)
{
if (Input.GetButton("Run") && !_motor.IsCrouching)
movement.Magnitude = 2.0f;
else
movement.Magnitude = 1.0f;
}
else
{
if (Input.GetButton("Run"))
movement.Magnitude = 1.0f;
else
movement.Magnitude = 0.5f;
}
}
_controller.MovementInput = movement;
}
c# unity3d
add a comment |
up vote
0
down vote
favorite
I want to Auto Move my character to following object, instead of keyboards controls, my code works fine on keyboards controls, I am trying to add a target as a gameobject and trying to character to follow it, "dummy" is variable is my public var, where I assign my target as a gameobject", here is my code so far,
protected virtual void UpdateMovement()
{
// var local = Input.GetAxis("Horizontal") * Vector3.right + Input.GetAxis("Vertical") * Vector3.forward;
//works fine at keyboards controls
var local = new Vector3(0,0,0);
local = dummy.transform.position - transform.position; // I tried this but did'nt work
transform.LookAt(dummy.transform);
var movement = new CharacterMovement();
movement.Direction = getMovementDirection(local);
if (WalkWhenZooming && _controller.ZoomInput)
{
movement.Magnitude = 0.5f;
movement.IsSlowedDown = true;
}
else
{
if ((_motor.ActiveWeapon.Gun != null || _motor.ActiveWeapon.HasMelee) && FastMovement)
{
if (Input.GetButton("Run") && !_motor.IsCrouching)
movement.Magnitude = 2.0f;
else
movement.Magnitude = 1.0f;
}
else
{
if (Input.GetButton("Run"))
movement.Magnitude = 1.0f;
else
movement.Magnitude = 0.5f;
}
}
_controller.MovementInput = movement;
}
c# unity3d
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to Auto Move my character to following object, instead of keyboards controls, my code works fine on keyboards controls, I am trying to add a target as a gameobject and trying to character to follow it, "dummy" is variable is my public var, where I assign my target as a gameobject", here is my code so far,
protected virtual void UpdateMovement()
{
// var local = Input.GetAxis("Horizontal") * Vector3.right + Input.GetAxis("Vertical") * Vector3.forward;
//works fine at keyboards controls
var local = new Vector3(0,0,0);
local = dummy.transform.position - transform.position; // I tried this but did'nt work
transform.LookAt(dummy.transform);
var movement = new CharacterMovement();
movement.Direction = getMovementDirection(local);
if (WalkWhenZooming && _controller.ZoomInput)
{
movement.Magnitude = 0.5f;
movement.IsSlowedDown = true;
}
else
{
if ((_motor.ActiveWeapon.Gun != null || _motor.ActiveWeapon.HasMelee) && FastMovement)
{
if (Input.GetButton("Run") && !_motor.IsCrouching)
movement.Magnitude = 2.0f;
else
movement.Magnitude = 1.0f;
}
else
{
if (Input.GetButton("Run"))
movement.Magnitude = 1.0f;
else
movement.Magnitude = 0.5f;
}
}
_controller.MovementInput = movement;
}
c# unity3d
I want to Auto Move my character to following object, instead of keyboards controls, my code works fine on keyboards controls, I am trying to add a target as a gameobject and trying to character to follow it, "dummy" is variable is my public var, where I assign my target as a gameobject", here is my code so far,
protected virtual void UpdateMovement()
{
// var local = Input.GetAxis("Horizontal") * Vector3.right + Input.GetAxis("Vertical") * Vector3.forward;
//works fine at keyboards controls
var local = new Vector3(0,0,0);
local = dummy.transform.position - transform.position; // I tried this but did'nt work
transform.LookAt(dummy.transform);
var movement = new CharacterMovement();
movement.Direction = getMovementDirection(local);
if (WalkWhenZooming && _controller.ZoomInput)
{
movement.Magnitude = 0.5f;
movement.IsSlowedDown = true;
}
else
{
if ((_motor.ActiveWeapon.Gun != null || _motor.ActiveWeapon.HasMelee) && FastMovement)
{
if (Input.GetButton("Run") && !_motor.IsCrouching)
movement.Magnitude = 2.0f;
else
movement.Magnitude = 1.0f;
}
else
{
if (Input.GetButton("Run"))
movement.Magnitude = 1.0f;
else
movement.Magnitude = 0.5f;
}
}
_controller.MovementInput = movement;
}
c# unity3d
c# unity3d
edited Nov 19 at 12:44
Basile Perrenoud
2,98111635
2,98111635
asked Nov 19 at 8:03
Newbies Studio
162213
162213
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
It doesn't work doesn't mean much. Is it crashing, showing an error, not moving, moving somewhere else than expected?
I assume the local
variable is a direction (so why not name it direction
?). The way you compute it seems correct. You can simplify the code by replacing
var local = new Vector3(0,0,0);
local = dummy.transform.position - transform.position;
with
var local = dummy.transform.position - transform.position;
If it works with the keyboard, most of your code must be correct. If your character moves way too fast, you can try normalising the direction vector
local = (dummy.transform.position - transform.position).normalized;
If it doesn't move or show an error, maybe the dummy isn't set properly. Do you see the expected position if you do
Debug.Log(dummy.transform.position)
Edit: You can also try to see if the direction is correct by adding
Debug.DrawLine(transform.position, transform.position + local)
yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
– Newbies Studio
Nov 19 at 10:38
And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
– Basile Perrenoud
Nov 19 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 at 12:21
but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
– Newbies Studio
Nov 19 at 12:22
Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
– Basile Perrenoud
Nov 19 at 12:49
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
It doesn't work doesn't mean much. Is it crashing, showing an error, not moving, moving somewhere else than expected?
I assume the local
variable is a direction (so why not name it direction
?). The way you compute it seems correct. You can simplify the code by replacing
var local = new Vector3(0,0,0);
local = dummy.transform.position - transform.position;
with
var local = dummy.transform.position - transform.position;
If it works with the keyboard, most of your code must be correct. If your character moves way too fast, you can try normalising the direction vector
local = (dummy.transform.position - transform.position).normalized;
If it doesn't move or show an error, maybe the dummy isn't set properly. Do you see the expected position if you do
Debug.Log(dummy.transform.position)
Edit: You can also try to see if the direction is correct by adding
Debug.DrawLine(transform.position, transform.position + local)
yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
– Newbies Studio
Nov 19 at 10:38
And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
– Basile Perrenoud
Nov 19 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 at 12:21
but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
– Newbies Studio
Nov 19 at 12:22
Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
– Basile Perrenoud
Nov 19 at 12:49
add a comment |
up vote
0
down vote
It doesn't work doesn't mean much. Is it crashing, showing an error, not moving, moving somewhere else than expected?
I assume the local
variable is a direction (so why not name it direction
?). The way you compute it seems correct. You can simplify the code by replacing
var local = new Vector3(0,0,0);
local = dummy.transform.position - transform.position;
with
var local = dummy.transform.position - transform.position;
If it works with the keyboard, most of your code must be correct. If your character moves way too fast, you can try normalising the direction vector
local = (dummy.transform.position - transform.position).normalized;
If it doesn't move or show an error, maybe the dummy isn't set properly. Do you see the expected position if you do
Debug.Log(dummy.transform.position)
Edit: You can also try to see if the direction is correct by adding
Debug.DrawLine(transform.position, transform.position + local)
yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
– Newbies Studio
Nov 19 at 10:38
And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
– Basile Perrenoud
Nov 19 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 at 12:21
but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
– Newbies Studio
Nov 19 at 12:22
Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
– Basile Perrenoud
Nov 19 at 12:49
add a comment |
up vote
0
down vote
up vote
0
down vote
It doesn't work doesn't mean much. Is it crashing, showing an error, not moving, moving somewhere else than expected?
I assume the local
variable is a direction (so why not name it direction
?). The way you compute it seems correct. You can simplify the code by replacing
var local = new Vector3(0,0,0);
local = dummy.transform.position - transform.position;
with
var local = dummy.transform.position - transform.position;
If it works with the keyboard, most of your code must be correct. If your character moves way too fast, you can try normalising the direction vector
local = (dummy.transform.position - transform.position).normalized;
If it doesn't move or show an error, maybe the dummy isn't set properly. Do you see the expected position if you do
Debug.Log(dummy.transform.position)
Edit: You can also try to see if the direction is correct by adding
Debug.DrawLine(transform.position, transform.position + local)
It doesn't work doesn't mean much. Is it crashing, showing an error, not moving, moving somewhere else than expected?
I assume the local
variable is a direction (so why not name it direction
?). The way you compute it seems correct. You can simplify the code by replacing
var local = new Vector3(0,0,0);
local = dummy.transform.position - transform.position;
with
var local = dummy.transform.position - transform.position;
If it works with the keyboard, most of your code must be correct. If your character moves way too fast, you can try normalising the direction vector
local = (dummy.transform.position - transform.position).normalized;
If it doesn't move or show an error, maybe the dummy isn't set properly. Do you see the expected position if you do
Debug.Log(dummy.transform.position)
Edit: You can also try to see if the direction is correct by adding
Debug.DrawLine(transform.position, transform.position + local)
edited Nov 19 at 10:56
answered Nov 19 at 9:06
Basile Perrenoud
2,98111635
2,98111635
yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
– Newbies Studio
Nov 19 at 10:38
And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
– Basile Perrenoud
Nov 19 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 at 12:21
but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
– Newbies Studio
Nov 19 at 12:22
Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
– Basile Perrenoud
Nov 19 at 12:49
add a comment |
yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
– Newbies Studio
Nov 19 at 10:38
And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
– Basile Perrenoud
Nov 19 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 at 12:21
but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
– Newbies Studio
Nov 19 at 12:22
Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
– Basile Perrenoud
Nov 19 at 12:49
yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
– Newbies Studio
Nov 19 at 10:38
yes u are right, I didn't clear my words, does not work means going in wrong direction. and Yes I debug it, my dummy variable is properly assigned.
– Newbies Studio
Nov 19 at 10:38
And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
– Basile Perrenoud
Nov 19 at 10:55
And what is this wrong direction like? random moves, opposite direction, straight line to wrong target? See the edit to the answer for another debug idea
– Basile Perrenoud
Nov 19 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 at 12:21
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 at 12:21
but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
– Newbies Studio
Nov 19 at 12:22
but character goes to position with awkward way, plz have a look at pic here imgur.com/a/JNS9Jwt blue line shows its way to go
– Newbies Studio
Nov 19 at 12:22
Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
– Basile Perrenoud
Nov 19 at 12:49
Then the error could be in CharacterMovement or in the _controller class. You pass the proper direction. The line you show in blue looks like the player is also moving somewhere else, so it goes a bit in the right direction but is pushed out of the way by some other script, then corrects a bit the next frame and so on. Look at the debug line when you use the key board. Is it correct? Then try to find how your controller class work and if another script changes the character movement
– Basile Perrenoud
Nov 19 at 12:49
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370521%2fauto-move-character-to-specific-target-instead-of-keyboard-controls-unity3d%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown