Bird Controller, Asceding and Descendng not working

Started by
1 comment, last by muhammad_hamid 11 months ago

Hi, so i m creating a 3rd person game, one level will be of our main players pet, which is a bird, so I m stuck on birds asceding and descend motion, it works fine for moving forward, backward, right left on the basis of our main camera direction. I have used the player controller, as we don't need the y direction in that, but i removed the code which sets the y direction 0. but it still doesn't work, the bird rotates to the direction where camera is facing in x,y,z direction but when i try to move it, it just moves, forward,back, right left, i want to move the bird forward backward but also considering the y axis, so that it should feels like its acsending or descending………Here my controller , i tried to add the camera.transform.up * vertical input, but its making unexpected behavior, Please if anyone can help…………

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EagleController : MonoBehaviour {
    
    [Header("Eagle Movement Section")]
    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private float runSpeed = 5f;
    [SerializeField] private float rotationSpeed = 2f;
    [SerializeField] private float acceleration = 2f;
    [SerializeField] private float velocityReducingFactor = 1f;
    [SerializeField] private float maxReducingValue = .3f;
    [SerializeField] private Rigidbody rb;
    [SerializeField] private Transform EagleChild;
    public Vector3 moveDirection = Vector3.zero;

    [Header("Animation Section")]
    public Animator animator;
    private void Awake() {
        //To make the cursor Invisible while playing
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
        rb = GetComponent<Rigidbody>();
    }


    private void Update() {
        Inputs();
        Walking();
       
    }



    private void Inputs() {
        //Taking input through axis
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        //Getting main camera transform, to use it for getting camera forward direction
        //This will then be used to rotate the player towards the camera face, Making the forward of the player= forward of camera
        Vector3 cameraForward = Camera.main.transform.forward;
        //cameraForward.y = 0f;
        cameraForward.Normalize();    // Normalizing to make it unit vector

        Vector3 cameraRight = Camera.main.transform.right;
        //cameraRight.y = 0f;
        cameraRight.Normalize();

        Vector3 cameraUP = Camera.main.transform.up;
        cameraUP.Normalize();

        


        moveDirection =cameraForward * verticalInput + cameraRight * horizontalInput;

        // Rotate the player towards the movement direction
        if (moveDirection != Vector3.zero)
            EagleChild.forward = Vector3.Slerp(EagleChild.forward, moveDirection, Time.deltaTime * rotationSpeed);

    }

    private void Walking() {
        Vector3 moveVelocity = moveDirection * moveSpeed;
        
        if (moveVelocity != Vector3.zero)
            velocityReducingFactor += acceleration * Time.deltaTime;

        else
            velocityReducingFactor -= acceleration * Time.deltaTime;


        velocityReducingFactor = Mathf.Clamp(velocityReducingFactor, maxReducingValue, 1);
        moveVelocity *= velocityReducingFactor;
        moveVelocity.y = rb.velocity.y;
        rb.velocity = Vector3.MoveTowards(rb.velocity, moveVelocity, 100 * Time.deltaTime);
        animator.SetBool("isFlying", moveDirection != Vector3.zero);

    }
}
Advertisement

Got the solution, the problem was in moveVelocity.y = rb.velocity.y, which was mainitaing y position and the bird couldn't ascend or descend

This topic is closed to new replies.

Advertisement