I’m a student, and almost a complete newb to coding, and I really would appreciate a quick fix! I need my cat to rotate, and turn towards the way the cat is going to run, and this is the enemy, so it’s all by code. Also, this is in 2019.4.8 and the rigidbody force thing doesn’t work from what I’ve tried. The cat is chasing the player(rabbit) and the cat moves, but I need him to rotate so he doesn’t end up running backwards. Any ideas??
public float speed;
private Rigidbody CatRb;
private GameObject Player;
// Start is called before the first frame update
void Start()
{
CatRb = GetComponent<Rigidbody>();
Player = GameObject.Find("Player");
}
// Update is called once per frame
void Update()
{
//getting the direction the cat should be moving towards
Vector3 lookDirection = (Player.transform.position - transform.position).normalized;
transform.Translate(lookDirection * speed);
}
This might create some weird rotations if the player transform and “this” transform is not placed at the same height in the map. It is easy to fix by creating a new position for the look direction before applying it as rotation:
Vector3 targetPositionXZ = new Vector3( player.transform.position.x, transform.position.y, player.transform.position.z ); //Copy of the player's position but uses the y-axis value of this instance
By passing the ( targetPositionXZ - transform.position ).normalized to the Quaternion.LookRotation your character will not start rotating it’s body up and down
How would I be able to plug this into the script so that I can make the Cat move in the desired direction? And would this be applicable to just plug into my player script as well? So that my character will start to run towards where my camera is facing? Also, it might be worth noting that my player only moves forwards and backwards, and it moves in the direction in which the camera is pointing, the camera revolves around the center point of the scene.
And I might as well add, my animator isn’t working so there’s that :'D
public class RabbitController : MonoBehaviour
{
private float speed = 100.0f;
private Rigidbody playerRb;
private GameObject focalPoint;
public float verticalInput;
private Animator m_animator;
public bool gameOver;
private string[] m_Running = new string[] {"RabbitRun"};
void Start()
{
playerRb = GetComponent<Rigidbody>();
focalPoint = GameObject.Find("Focal Point");
m_animator = GetComponent<Animator>();
}
void Update()
{
//takes player input and adds "forward" force
float verticalInput = Input.GetAxis("Vertical");
//this says .right because .forward would move it left and right. So I figured right must move it froward and backward
if (Input.GetKeyDown(KeyCode.UpArrow));
{
transform.Translate(focalPoint.transform.right * verticalInput * Time.deltaTime * speed);
//m_animator.SetInteger("Run");
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
Debug.Log("Game Over!");
gameOver = true;
m_animator.SetBool("Death_b", true);
m_animator.SetInteger("RabbitDead", 1);
}
}
}
Wow, that’s about 5 or 6 things all at once. Nobody can work like that.
The way software engineering works best is through iterative development.
You get one thing working, you commit it to source control.
You work on the next thing, commit it to source control.
The first time something screws up you instantly begin focusing on fixing it.
If you cannot fix it, you revert to a previous state of the source control and re-try your efforts.
It sounds like you probably want to start with several basic tutorials for the key steps you need for your game design: movement, animation, turning, perhaps some camera work. Each of those is a separate thing. Handle them separately, iteratively approach the problems and solve them one at a time.
In all coding, you break down large complex problems into a series of small problems. You solve each of the small problems individually. You test each one. Once you’ve completed all the small problems, you test that the whole system you’ve now built solves the original large problem.
In your OP you were talking about a mouse and a cat, but this is called RabbitController, so it isn’t clear to me what this script is supposed to be. Also it doesn’t appear to have anything you mentioned you need in it, so hard to help.
Yes so looking back at that I can see how this can all be very confusing, so I edited the OP and it’s supposed to be a rabbit, but I keep thinking mouse. And I know coding is a lot of small problems, but all the tutorials I find are all 2018 or don’t address my problem or both. And I’m a bit of a ranter, so once I start writing or typing I often find that I just zone out and work, and than I stop and I’ve completely switched subject sometimes, so thanks but also sorry for it being confusing. If you have any suggestions on tutorials that would be helpful, also The second script was of my Player(Rabbit) controller because I was also wondering if I could use the same code to rotate my player in the right direction, but that’ll come later I think.