x


How would I make an object move forward based on the camera attached to it?

Hello, I'm a student working on my Game Production 1 final. I'm trying to make a character, move forward in relative to the camera parented to it. I tried using "var fowardDirection = Camera.main.transform.TransformDirection(Vector3.forward)" in order to link the camera and the character but the result a warning: Unused Local Variable, and the mechanic not working in the Unity Editor. This is the code I'm currently using.

function Awake()
{
    controller = GetComponent(CharacterController);
}

function Update () 
{
    if(controller.isGrounded)
    {
        if(charge <= maxCharge && Input.GetButton("Jump"))
        {
            charge++;
            print(charge);
        }
         else if(Input.GetButtonUp)
         {
            charge = 0;
         }

        moveDirection = Vector3(0, 0,Input.GetAxis("Vertical"));
        //moveDirection.z = Input.GetAxis("Vertical");
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= moveSpeed;

        Jumping();
        RotateChar();
    }
    Burst();

    moveDirection.y -= gravity * Time.deltaTime;

    controller.Move(moveDirection);    
    visualSphere.transform.Rotate(Vector3(moveDirection.x, moveDirection.z, 0));
}

function Jumping()
{
    if (canJump != maxJumps)
    {
        if (charge >= maxCharge && Input.GetButtonUp("Jump"))
            {
                moveDirection.y = superJumpSpeed * Time.deltaTime;

            }
            else if (Input.GetButtonUp("Jump"))
            {
                moveDirection.y = jumpSpeed * Time.deltaTime;
            }
    }
}
public function RotateChar()
{
    transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime,0);
}

function Burst()
{
var fowardDirection = Camera.main.transform.TransformDirection(Vector3.forward);

    if(Input.GetButton("Fire1"))
    {
        forwardDirection = force * Time.deltaTime;
    }

The problem is the burst function at the end. When you click and hold the "fire1" button, the character supposed to shoot forward. Am I perhaps missing something somewhere in the code, or perhaps am doing this the wrong way?

more ▼

asked Oct 02, 2012 at 03:45 AM

Yunglex313 gravatar image

Yunglex313
1 6 4 4

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

What about Camera.main.transform.forward?

In the future, please post the least amount of code as is possible. Sometimes it gets hard to read an entire page worth when you're not sure what part is important to the question.

Thank you.

more ▼

answered Oct 02, 2012 at 05:21 AM

Muuskii gravatar image

Muuskii
1.3k 4 7 18

Didn't work, and I'm still getting the "Unused Local Variable" where forwardDirection is used.

Oct 02, 2012 at 03:29 PM Yunglex313
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4630
x145
x24

asked: Oct 02, 2012 at 03:45 AM

Seen: 1067 times

Last Updated: Oct 02, 2012 at 03:29 PM