In unity, set transform.forward cause transform.position change
I set transform.forward perfect in horizontal move, but when i use it in vertical move, see my gif, transform.forward = newForward
will cause transform.position change, how to make character forward but don't change transform.position
here is my full script:
public class TestVerticalMoveForward : MonoBehaviour
{
private GameObject from;
private GameObject to;
private GameObject target;
// Use this for initialization
void Start()
{
from = GameObject.Find("From");
to = GameObject.Find("To");
target = from;
}
private float speed = 10;
// Update is called once per frame
void Update()
{
var offset = target.transform.position - transform.position;
var forward = offset.normalized;
var offset2 = speed * Time.deltaTime * forward;
if (offset.magnitude <= offset2.magnitude)
{
transform.position = target.transform.position;
if (target == to)
{
target = from;
}
else
{
target = to;
}
}
else
{
transform.position += offset2;
}
if (offset != Vector3.zero)
{
transform.forward = offset;
}
}
}
c# unity3d
add a comment |
I set transform.forward perfect in horizontal move, but when i use it in vertical move, see my gif, transform.forward = newForward
will cause transform.position change, how to make character forward but don't change transform.position
here is my full script:
public class TestVerticalMoveForward : MonoBehaviour
{
private GameObject from;
private GameObject to;
private GameObject target;
// Use this for initialization
void Start()
{
from = GameObject.Find("From");
to = GameObject.Find("To");
target = from;
}
private float speed = 10;
// Update is called once per frame
void Update()
{
var offset = target.transform.position - transform.position;
var forward = offset.normalized;
var offset2 = speed * Time.deltaTime * forward;
if (offset.magnitude <= offset2.magnitude)
{
transform.position = target.transform.position;
if (target == to)
{
target = from;
}
else
{
target = to;
}
}
else
{
transform.position += offset2;
}
if (offset != Vector3.zero)
{
transform.forward = offset;
}
}
}
c# unity3d
If you only want to rotate the object without moving it then don't change the position like:transform.position += offset2;
– Iggy
Nov 11 at 13:24
Yes, I know this way, but in most case, I should make character face target, I guess I can manually rotate character to instead of transform.forward, but I think it's a little difficult
– asullaherc
Nov 11 at 13:36
add a comment |
I set transform.forward perfect in horizontal move, but when i use it in vertical move, see my gif, transform.forward = newForward
will cause transform.position change, how to make character forward but don't change transform.position
here is my full script:
public class TestVerticalMoveForward : MonoBehaviour
{
private GameObject from;
private GameObject to;
private GameObject target;
// Use this for initialization
void Start()
{
from = GameObject.Find("From");
to = GameObject.Find("To");
target = from;
}
private float speed = 10;
// Update is called once per frame
void Update()
{
var offset = target.transform.position - transform.position;
var forward = offset.normalized;
var offset2 = speed * Time.deltaTime * forward;
if (offset.magnitude <= offset2.magnitude)
{
transform.position = target.transform.position;
if (target == to)
{
target = from;
}
else
{
target = to;
}
}
else
{
transform.position += offset2;
}
if (offset != Vector3.zero)
{
transform.forward = offset;
}
}
}
c# unity3d
I set transform.forward perfect in horizontal move, but when i use it in vertical move, see my gif, transform.forward = newForward
will cause transform.position change, how to make character forward but don't change transform.position
here is my full script:
public class TestVerticalMoveForward : MonoBehaviour
{
private GameObject from;
private GameObject to;
private GameObject target;
// Use this for initialization
void Start()
{
from = GameObject.Find("From");
to = GameObject.Find("To");
target = from;
}
private float speed = 10;
// Update is called once per frame
void Update()
{
var offset = target.transform.position - transform.position;
var forward = offset.normalized;
var offset2 = speed * Time.deltaTime * forward;
if (offset.magnitude <= offset2.magnitude)
{
transform.position = target.transform.position;
if (target == to)
{
target = from;
}
else
{
target = to;
}
}
else
{
transform.position += offset2;
}
if (offset != Vector3.zero)
{
transform.forward = offset;
}
}
}
c# unity3d
c# unity3d
asked Nov 11 at 10:25
asullaherc
747826
747826
If you only want to rotate the object without moving it then don't change the position like:transform.position += offset2;
– Iggy
Nov 11 at 13:24
Yes, I know this way, but in most case, I should make character face target, I guess I can manually rotate character to instead of transform.forward, but I think it's a little difficult
– asullaherc
Nov 11 at 13:36
add a comment |
If you only want to rotate the object without moving it then don't change the position like:transform.position += offset2;
– Iggy
Nov 11 at 13:24
Yes, I know this way, but in most case, I should make character face target, I guess I can manually rotate character to instead of transform.forward, but I think it's a little difficult
– asullaherc
Nov 11 at 13:36
If you only want to rotate the object without moving it then don't change the position like:
transform.position += offset2;
– Iggy
Nov 11 at 13:24
If you only want to rotate the object without moving it then don't change the position like:
transform.position += offset2;
– Iggy
Nov 11 at 13:24
Yes, I know this way, but in most case, I should make character face target, I guess I can manually rotate character to instead of transform.forward, but I think it's a little difficult
– asullaherc
Nov 11 at 13:36
Yes, I know this way, but in most case, I should make character face target, I guess I can manually rotate character to instead of transform.forward, but I think it's a little difficult
– asullaherc
Nov 11 at 13:36
add a comment |
1 Answer
1
active
oldest
votes
I find the solution and reason,
solution:
here is fixed result
I use Quaternion.LookRotation
instead of transform.forward=
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using Script.Ro.Ext;
using UnityEngine;
public class TestVerticalMoveForward : MonoBehaviourExt
{
private GameObject from;
private GameObject to;
private GameObject target;
// private GameObject center;
// Use this for initialization
void Start()
{
from = GameObject.Find("From");
to = GameObject.Find("To");
// center = GameObject.Find("Character/Center");
target = from;
}
private float speed = 10;
// Update is called once per frame
void Update()
{
Watch("center.transform.position", transform.position);
var offset = target.transform.position - transform.position;
var forward = offset.normalized;
var offset2 = speed * Time.deltaTime * forward;
if (offset.magnitude <= offset2.magnitude)
{
transform.position += offset;
if (target == to)
{
target = from;
}
else
{
target = to;
}
}
else
{
transform.position += offset2;
}
if (offset != Vector3.zero)
{
var upAxis = transform.rotation * Vector3.up;
transform.rotation =
Quaternion.LookRotation(target.transform.position - transform.position, upAxis);
}
}
}
#endif
reason
unity has
Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up);
I guess transform.forward =
invoke internal LookRotation code and always set upwards = Vector3.up
(y axis), in horizontal move, it work perfect, but in vertical move, it perform unexpectly
so imagine object has center-up-axis, for ex, to humanoid object, the up axis, is body center to head center axis, to make sure vertical move perform like horizontal move, LookRotation's upwards should always be center-up-axis, see my above code, this center-up-axis should be:
var upAxis = transform.rotation * Vector3.up ;
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%2f53247811%2fin-unity-set-transform-forward-cause-transform-position-change%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
I find the solution and reason,
solution:
here is fixed result
I use Quaternion.LookRotation
instead of transform.forward=
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using Script.Ro.Ext;
using UnityEngine;
public class TestVerticalMoveForward : MonoBehaviourExt
{
private GameObject from;
private GameObject to;
private GameObject target;
// private GameObject center;
// Use this for initialization
void Start()
{
from = GameObject.Find("From");
to = GameObject.Find("To");
// center = GameObject.Find("Character/Center");
target = from;
}
private float speed = 10;
// Update is called once per frame
void Update()
{
Watch("center.transform.position", transform.position);
var offset = target.transform.position - transform.position;
var forward = offset.normalized;
var offset2 = speed * Time.deltaTime * forward;
if (offset.magnitude <= offset2.magnitude)
{
transform.position += offset;
if (target == to)
{
target = from;
}
else
{
target = to;
}
}
else
{
transform.position += offset2;
}
if (offset != Vector3.zero)
{
var upAxis = transform.rotation * Vector3.up;
transform.rotation =
Quaternion.LookRotation(target.transform.position - transform.position, upAxis);
}
}
}
#endif
reason
unity has
Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up);
I guess transform.forward =
invoke internal LookRotation code and always set upwards = Vector3.up
(y axis), in horizontal move, it work perfect, but in vertical move, it perform unexpectly
so imagine object has center-up-axis, for ex, to humanoid object, the up axis, is body center to head center axis, to make sure vertical move perform like horizontal move, LookRotation's upwards should always be center-up-axis, see my above code, this center-up-axis should be:
var upAxis = transform.rotation * Vector3.up ;
add a comment |
I find the solution and reason,
solution:
here is fixed result
I use Quaternion.LookRotation
instead of transform.forward=
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using Script.Ro.Ext;
using UnityEngine;
public class TestVerticalMoveForward : MonoBehaviourExt
{
private GameObject from;
private GameObject to;
private GameObject target;
// private GameObject center;
// Use this for initialization
void Start()
{
from = GameObject.Find("From");
to = GameObject.Find("To");
// center = GameObject.Find("Character/Center");
target = from;
}
private float speed = 10;
// Update is called once per frame
void Update()
{
Watch("center.transform.position", transform.position);
var offset = target.transform.position - transform.position;
var forward = offset.normalized;
var offset2 = speed * Time.deltaTime * forward;
if (offset.magnitude <= offset2.magnitude)
{
transform.position += offset;
if (target == to)
{
target = from;
}
else
{
target = to;
}
}
else
{
transform.position += offset2;
}
if (offset != Vector3.zero)
{
var upAxis = transform.rotation * Vector3.up;
transform.rotation =
Quaternion.LookRotation(target.transform.position - transform.position, upAxis);
}
}
}
#endif
reason
unity has
Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up);
I guess transform.forward =
invoke internal LookRotation code and always set upwards = Vector3.up
(y axis), in horizontal move, it work perfect, but in vertical move, it perform unexpectly
so imagine object has center-up-axis, for ex, to humanoid object, the up axis, is body center to head center axis, to make sure vertical move perform like horizontal move, LookRotation's upwards should always be center-up-axis, see my above code, this center-up-axis should be:
var upAxis = transform.rotation * Vector3.up ;
add a comment |
I find the solution and reason,
solution:
here is fixed result
I use Quaternion.LookRotation
instead of transform.forward=
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using Script.Ro.Ext;
using UnityEngine;
public class TestVerticalMoveForward : MonoBehaviourExt
{
private GameObject from;
private GameObject to;
private GameObject target;
// private GameObject center;
// Use this for initialization
void Start()
{
from = GameObject.Find("From");
to = GameObject.Find("To");
// center = GameObject.Find("Character/Center");
target = from;
}
private float speed = 10;
// Update is called once per frame
void Update()
{
Watch("center.transform.position", transform.position);
var offset = target.transform.position - transform.position;
var forward = offset.normalized;
var offset2 = speed * Time.deltaTime * forward;
if (offset.magnitude <= offset2.magnitude)
{
transform.position += offset;
if (target == to)
{
target = from;
}
else
{
target = to;
}
}
else
{
transform.position += offset2;
}
if (offset != Vector3.zero)
{
var upAxis = transform.rotation * Vector3.up;
transform.rotation =
Quaternion.LookRotation(target.transform.position - transform.position, upAxis);
}
}
}
#endif
reason
unity has
Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up);
I guess transform.forward =
invoke internal LookRotation code and always set upwards = Vector3.up
(y axis), in horizontal move, it work perfect, but in vertical move, it perform unexpectly
so imagine object has center-up-axis, for ex, to humanoid object, the up axis, is body center to head center axis, to make sure vertical move perform like horizontal move, LookRotation's upwards should always be center-up-axis, see my above code, this center-up-axis should be:
var upAxis = transform.rotation * Vector3.up ;
I find the solution and reason,
solution:
here is fixed result
I use Quaternion.LookRotation
instead of transform.forward=
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using Script.Ro.Ext;
using UnityEngine;
public class TestVerticalMoveForward : MonoBehaviourExt
{
private GameObject from;
private GameObject to;
private GameObject target;
// private GameObject center;
// Use this for initialization
void Start()
{
from = GameObject.Find("From");
to = GameObject.Find("To");
// center = GameObject.Find("Character/Center");
target = from;
}
private float speed = 10;
// Update is called once per frame
void Update()
{
Watch("center.transform.position", transform.position);
var offset = target.transform.position - transform.position;
var forward = offset.normalized;
var offset2 = speed * Time.deltaTime * forward;
if (offset.magnitude <= offset2.magnitude)
{
transform.position += offset;
if (target == to)
{
target = from;
}
else
{
target = to;
}
}
else
{
transform.position += offset2;
}
if (offset != Vector3.zero)
{
var upAxis = transform.rotation * Vector3.up;
transform.rotation =
Quaternion.LookRotation(target.transform.position - transform.position, upAxis);
}
}
}
#endif
reason
unity has
Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up);
I guess transform.forward =
invoke internal LookRotation code and always set upwards = Vector3.up
(y axis), in horizontal move, it work perfect, but in vertical move, it perform unexpectly
so imagine object has center-up-axis, for ex, to humanoid object, the up axis, is body center to head center axis, to make sure vertical move perform like horizontal move, LookRotation's upwards should always be center-up-axis, see my above code, this center-up-axis should be:
var upAxis = transform.rotation * Vector3.up ;
answered Nov 11 at 15:03
asullaherc
747826
747826
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53247811%2fin-unity-set-transform-forward-cause-transform-position-change%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
If you only want to rotate the object without moving it then don't change the position like:
transform.position += offset2;
– Iggy
Nov 11 at 13:24
Yes, I know this way, but in most case, I should make character face target, I guess I can manually rotate character to instead of transform.forward, but I think it's a little difficult
– asullaherc
Nov 11 at 13:36