#Gameobject Rotates on wrong axes
1 messages · Page 1 of 1 (latest)
you can just use
transform.LookAt(target);
so in your case it would be
Transform player = GameObject.Find("Player").transform;
transform.LookAt(player);
You can read more about it here
Hope this helps
With that code the Bot is now only rotating on the Y Axes
transform.rotation is now x=0 y=90 z=0
Thats wierd
Maybe you have to set an extra direction
I tried some things with the New code
yeah thats what i tried
I works perfetct and always rotates to the player
just the axes is wrong haha
hows that
wait
Quaternion rot = transform.LookAt(player) + new Quaternion(x, y, z, 0)
transform.rotation = rot
this should work
but i am not sure
and then you add like 90° on the x axis or stuff
hmm
does it work
the offset
like +90° on x
if you dont wanna change for example the z axis change z to 0
idk how it looks in your game i just know that the axis are messed up
and an offset could help
maybe send some screenshots
it would help
yeah yeah 1 sec
Default Bot Transform
Executed with lookat(Player)
3D View Executed
wait is it a 3d or 2d game
2d top down
i thought it was a 3d game
no😕
good idea
ohh
it does in fact now rotate the Z axes
but y aswell
I tried to do transform.rotation = transform.rotation + new Quaternion(0,-90,0);
but it wont work 😦
did you try both
forward and back
maybe the player is higher then bot
thats why he looks up
so, i tried some more things
i tried both forward and backward
now what i did was: transform.right = PlayerTransform.position - transform.position;
and it kinda works
KINDa
GOT IT
just need to modify something
sure
nah np
how do i set my for example: Z Rotation +90 degrees
so can you send me your code part
take your time
transform.right = transform.position - PlayerTransform.position;
thats it
no more needed
now its just off by 90 degrees
is there any way to change the transform.roration.z by 90 degrees?
didnt know that you can set transform.right
same
transform.rotation.z += 90
i did exactly that but it telly my that transform.rotation isnt a changable variable
Assets\Scripts\Bot Scripts\Bot_Behavior.cs(49,9): error CS1612: Cannot modify the return value of 'Transform.rotation' because it is not a variable
transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, 90, 0);
and does it work?
it locks it to 180 somehow
even if i do transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y,transform.rotation.z + 90, 0);
then try it with euler and local
transform.localRotation = Quaternion.Euler(transform.localRotation.x, transform.localRotation.y, 90);
totaly forgot about local
yes same
when i do transform.localRotation = Quaternion.Euler(transform.localRotation.x, transform.localRotation.y, 90); it will lock it to 90 on z. but if i do transform.localRotation = Quaternion.Euler(transform.localRotation.x, transform.localRotation.y, transform.localRotation.z + 90f); it will move it by 1 degrees depending on which side i am
what do you wanna achieve
that is has an offset of 90 degree
transform.right = PlayerTransform.position - transform.position;
this is all your code?
The bot is now facing at me with his side if i would rotate him by 90 degrees i would line him up to face me in my eyes to his eyes
yk?
yes
can you send me all your look code
thats all of the code i need to rotate him in my dir
wait how do i tell dc its a cs script again
k
then code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bot_Behavior : MonoBehaviour
{
void Awake() {
BewegungsängerungsZeit = 30f;
//StartCoroutine(Bot_Random_Move());
}
void Start()
{
World = GameObject.Find("World");
animatior = gameObject.GetComponent<Animator>();
PlayerTransform = GameObject.Find("Player").transform;
}
void Update()
{
//Simples Bot Movement
float horizontalInput = X * speed * Time.deltaTime;
float verticalInput = Y * speed * Time.deltaTime;
transform.Translate(verticalInput/*X*/,horizontalInput/*Y*/,0f/*Z*/);
//Bot dreht sich in die Richtung in die er sich bewegt.
/*if(new Vector2(horizontalInput,verticalInput) != Vector2.zero){
Quaternion toRotation = Quaternion.LookRotation(Vector3.forward, new Vector2(horizontalInput,verticalInput));
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 700f);
}*/
//KONZEPT FÜR "BOT VISIERT SPIELER AN"
transform.right = transform.position - PlayerTransform.position;
//Animatior
if(transform.position != lastpos){
animatior.SetBool("isrunning", true);
}else{
animatior.SetBool("isrunning", false);
}
lastpos = transform.position;
Weapon_Follow_Player();
}
public void Weapon_Follow_Player(){
Weapons.transform.position = transform.position;
Weapons.transform.rotation = transform.rotation;
}
public IEnumerator Bot_Random_Move(){
for(Zyklus = 1; ; Zyklus++){
X = Random.Range(1f,10f) / 10f;
Y = Random.Range(1f,10f) / 10f;
if(Zyklus == 6) BewegungsängerungsZeit = 15f;
yield return new WaitForSeconds(BewegungsängerungsZeit);
}
}
}```
gotta remove all variable declarations at the start to put it in here
hätte ich bloß gewusst das du deutsch bist
pahahahahahaaha
;)
🤣
wäre es schlimm wenn du einen rigidbody auf dem spieler hinzufügen müsstest
es würde vieles erleichtern und außer führt das auch zu smootheren ergebnissen
ah perfekt
meinst du ich sollte mein movement auf meinen rb überschreiben?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bot_Behavior : MonoBehaviour
{
Vector2 movement; //--NEU
Rigidbody2D rb; //--NEU
void Awake() {
BewegungsängerungsZeit = 30f;
//StartCoroutine(Bot_Random_Move());
}
void Start()
{
World = GameObject.Find("World");
animatior = gameObject.GetComponent<Animator>();
PlayerTransform = GameObject.Find("Player").transform;
rb = GetComponent<Rigidbody2D>(); //--NEU
}
void Update()
{
//Simples Bot Movement
movement.x = Input.GetAxiRaw("Horizontal"); //--NEU
movement.y = Input.GetAxiRaw("Vertical"); //--NEU
//Animatior
if(transform.position != lastpos){
animatior.SetBool("isrunning", true);
}else{
animatior.SetBool("isrunning", false);
}
lastpos = transform.position;
Weapon_Follow_Player();
}
void FixedUpdate() //--NEU
{
rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime); //--NEU
Vector2 lookDir = PlayerTransform.position - rb.position; //--NEU
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg + 90f; //--NEU
rb.rotation = angle; //--NEU
}
public void Weapon_Follow_Player(){
Weapons.transform.position = transform.position;
Weapons.transform.rotation = transform.rotation;
}
public IEnumerator Bot_Random_Move(){
for(Zyklus = 1; ; Zyklus++){
X = Random.Range(1f,10f) / 10f;
Y = Random.Range(1f,10f) / 10f;
if(Zyklus == 6) BewegungsängerungsZeit = 15f;
yield return new WaitForSeconds(BewegungsängerungsZeit);
}
}
}```
probier das
hmm
wenn irgendwas unklar ist frag einfach
Ich kann nicht PlayerTransform.position - rb.position nehmen
eins ist ein vector 3 anderer 2
dann mach
Vector2 playerPos = new Vector2(playertransform.postion.x, playertransform.postion.y);
okay
genau so hab ichs
aaber
ich krieg nen error
rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);
da soll ne null reference exeption sein
komme nicht drauf
ah ne
habs gefixt
okay geht es jetzt
ja musste nur bisschen was ändern
aber vielen dank für deine Zeit und Hilfe!
Muss jetzt nur bisschen schauen das ich alles verstehe was da neu ist und dann kann ich weiter machen 🙂
hab gerne geholfen
ja ich weiß das umgucken sieht ein wenig kompliziert aus auch wegen den mathematischen funktion aber eigentlich ist es ziemlich simple
solltest du trotzdem noch fragen haben kannst du mich auch dm