#Gameobject Rotates on wrong axes

1 messages · Page 1 of 1 (latest)

boreal matrix
#

It actually rotates on both axes

restive silo
#

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

boreal matrix
#

transform.rotation is now x=0 y=90 z=0

restive silo
#

Maybe you have to set an extra direction

boreal matrix
#

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

restive silo
#

you could add an extra quaternion to it

#

to fix the axis problem

boreal matrix
#

hows that

restive silo
#

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

boreal matrix
#

hmm

restive silo
#

does it work

boreal matrix
#

hold on

#

what do you mean with x,y,z

restive silo
#

the offset

#

like +90° on x

#

if you dont wanna change for example the z axis change z to 0

boreal matrix
#

so 0,270,0?

#

but it wont help the z axes to rotate

restive silo
#

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

boreal matrix
#

yeah yeah 1 sec

#

Default Bot Transform

#

Executed with lookat(Player)

#

3D View Executed

restive silo
#

wait is it a 3d or 2d game

boreal matrix
#

2d top down

restive silo
#

why dont you say that

#

bruuh

boreal matrix
#

its just the 3d view of the 2d game

#

lol

#

why?

restive silo
#

i thought it was a 3d game

boreal matrix
#

no😕

restive silo
#

try

#
transform.LookAt(player, Vector3.back)
boreal matrix
#

good idea

restive silo
#
transform.LookAt(player, Vector3.forward)
#

or this

boreal matrix
#

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 😦

restive silo
#

did you try both

#

forward and back

#

maybe the player is higher then bot

#

thats why he looks up

boreal matrix
#

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

restive silo
#

thats great

#

gl on your project

boreal matrix
#

thx a lot for help

#

i got one last question

restive silo
restive silo
boreal matrix
#

how do i set my for example: Z Rotation +90 degrees

restive silo
#

so can you send me your code part

boreal matrix
#

sure

#

give me 10 mins i gotta help my dad

restive silo
#

take your time

boreal matrix
#

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?

restive silo
#

didnt know that you can set transform.right

boreal matrix
#

same

restive silo
#
transform.rotation.z += 90
boreal matrix
#

i did exactly that but it telly my that transform.rotation isnt a changable variable

restive silo
#

whats the error code

#

if not try:

boreal matrix
#

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

restive silo
#
transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, 90, 0);
#

and does it work?

boreal matrix
#

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);

restive silo
#

then try it with euler and local

#
transform.localRotation = Quaternion.Euler(transform.localRotation.x, transform.localRotation.y, 90);
boreal matrix
#

totaly forgot about local

restive silo
#

yes same

boreal matrix
#

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

restive silo
#

what do you wanna achieve

#

that is has an offset of 90 degree

#
transform.right = PlayerTransform.position - transform.position;
#

this is all your code?

boreal matrix
#

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?

restive silo
#

yes

boreal matrix
#

90 turn and i would have it

restive silo
#

can you send me all your look code

boreal matrix
restive silo
#

but you have to set the transfrom.right somewhere

#

or am i wrong

boreal matrix
#

wait how do i tell dc its a cs script again

restive silo
#

three of them

#

then cs

boreal matrix
#

k

restive silo
#

then code

boreal matrix
#
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

restive silo
#

hätte ich bloß gewusst das du deutsch bist

boreal matrix
#

pahahahahahaaha

restive silo
#

;)

boreal matrix
#

🤣

restive silo
#

wäre es schlimm wenn du einen rigidbody auf dem spieler hinzufügen müsstest

boreal matrix
#

überhaupt nicht

#

ist sogar eigentlich schon

restive silo
#

es würde vieles erleichtern und außer führt das auch zu smootheren ergebnissen

restive silo
boreal matrix
#

meinst du ich sollte mein movement auf meinen rb überschreiben?

restive silo
#
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

boreal matrix
#

hmm

restive silo
#

wenn irgendwas unklar ist frag einfach

boreal matrix
#

Ich kann nicht PlayerTransform.position - rb.position nehmen

#

eins ist ein vector 3 anderer 2

restive silo
#

dann mach

boreal matrix
#

habs

#

moment

restive silo
#
Vector2 playerPos = new Vector2(playertransform.postion.x, playertransform.postion.y);
restive silo
boreal matrix
#

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

restive silo
#

okay geht es jetzt

boreal matrix
#

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 🙂

restive silo
restive silo
#

solltest du trotzdem noch fragen haben kannst du mich auch dm