Auto Move character to specific target instead of keyboard controls 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
add a comment |
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 |
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 '18 at 12:44
Basile Perrenoud
3,14311737
3,14311737
asked Nov 19 '18 at 8:03
Newbies StudioNewbies Studio
167313
167313
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 '18 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 '18 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 '18 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 '18 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 '18 at 12:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 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 '18 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 '18 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 '18 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 '18 at 12:49
add a comment |
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 '18 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 '18 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 '18 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 '18 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 '18 at 12:49
add a comment |
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 '18 at 10:56
answered Nov 19 '18 at 9:06
Basile PerrenoudBasile Perrenoud
3,14311737
3,14311737
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 '18 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 '18 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 '18 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 '18 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 '18 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 '18 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 '18 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 10:55
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 '18 at 12:21
Debug.DrawLine showing desire result, here you can see imgur.com/a/NztU0LX
– Newbies Studio
Nov 19 '18 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 '18 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 '18 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 '18 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 '18 at 12:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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