Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Creature rotates -90 degrees on X and 180 on y upon start and will not unrotate. And other issues.

Discussion in 'Scripting' started by ThirdEyeBlindGames, Aug 28, 2023.

  1. ThirdEyeBlindGames

    ThirdEyeBlindGames

    Joined:
    Aug 17, 2023
    Posts:
    14
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class monster_movement : MonoBehaviour
    6. {
    7.     private Animator animator;
    8.     private Transform target;
    9.     public GameObject Player;
    10.     private Transform LookingAt;
    11.     private Transform targetlook;
    12.  
    13.     [SerializeField]
    14.     float Speed = 5.0f;
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         animator = GetComponent<Animator>();
    19.         thingy = GetComponent<Capsule Collider>;
    20.         thingy.Transform.rotation = 90, 0, 0;
    21.         Player = GameObject.FindWithTag("Player");
    22.         target = Player.transform;
    23.     }
    24.     void Update()
    25.     {
    26.         transform.position = Vector3.MoveTowards(transform.position, Player.transform.position, Speed * Time.deltaTime);  
    27.         transform.LookAt(1 * transform.position - target.position);
    28.         transform.LookAt(Player.transform);
    29.         transform.rotation = Quaternion.Euler(transform.rotation.x + 90, transform.rotation.y - 180, transform.rotation.z);
    30.         // deleted -180f after .y to see if that will return monster to facing properly
    31.        transform.rotation = Quaternion.LookRotation(Vector3.up, transform.position - target.position);
    32.       //transform.position = Vector3.MoveTowards(transform.position, Player.transform.position, Speed * Time.deltaTime);
    33.         if (Speed > 0f)
    34.         {
    35.             animator.SetBool("isRunning", true);
    36.         }  ;
    37.         if (Speed < 0f)
    38.         {
    39.             animator.SetBool("isRunning", false);
    40.         }  ;  
    41.     }
    42. }

    My creature loads into unity with a rotation of 0,0,0. The MOMENT I press play, it flings itself to -90, 0, (wherever the player is so it's facing the player.) Locking the rigid body hasn't helped, and as you can see in my code, I've tried locking the X rotation to a 90 to try to cancel that -90 out but it won't work. Any help I can get on this stupid pain in my butt would be greatly appreciated. I've been working on JUST this idiot alone for what is roughly 4-5 months collectively. (The second semester last school year and every day so far of this school year. Gotta love trying everything you have and none of them work, and some make more issues than what you started with...) I'm having issue after issue with him, when I just want to make him do 4 simple tasks;
    1: Chase player when player is within range.
    2: Wander aimlessly when player is not in range.
    3: End game on collision with player
    4: Face forward when in motion, no matter if its chasing or wandering

    I know it's not an issue in Blender (the platform I built this moron on) because I had it completing 1&4 at some point but I have no idea how I did it nor do I know what I need to do to have all 4 tasks running smoothly and properly all at the same time. If anyone knows what I did or need to still do, I'd be forever grateful for your help.

    Thanks,
    An amateur dev running on sheer spite, frustration and determination.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    ^ ^ ^ ^ ^ None of THAT code is compiling! GO FIX YOUR ERRORS. See below.

    Sounds like you left an animation in place that shouldn't be there. Or else your existing animation is driving too many properties. Look at the various animations you have in the Animation window to see what properties they manipulate. Also make sure that "Write Defaults" isn't enabled in the various states you're seeing the problem in.

    If that's not it then try rebuilding your player prefab one step at a time, perhaps starting with just a capsule until all the logic is operational.

    Otherwise, how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?

    You should try this approach: simply ask yourself "Can I ...?" again and again. It works quite well.


    Imphenzia: How Did I Learn To Make Games:





    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:



    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
    Last edited: Aug 28, 2023
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    line 20 has Transform, which should be transform..
    player should be lowercase, so you don't confuse yourself on difference between class and variable..
    you have extra semi-colons on lines 36 and 40..
    line 28 should just be target, as you already declared that..

    And as far as your 4 rotation calls to the transform, I have no clue what any of that is. There should be one call to position, and one call to rotation, or better yet one single call for the both of them
    transform.SetPositionAndRotation(pos, rot);
    ..

    Yeah, I don't what you're trying to accomplish, there's just so many errors, and beating-around-the-bush going on here.
     
    Kurt-Dekker likes this.