Luke Stoward
Luke Stoward

Reputation: 1540

Move object forward in facing direction

basically I am trying to make my character dash forward a specific distance in the direction the camera is facing. This a fps and the best I have been able to do so face is dash forward in the global X axes, which obviously is wrong.

I have this function which is called from the update function:

Debug.Log("We are dashing.");

    if (!inputDash || !canControl)
        dashing.lastButtonDownTime = -100;

    if (inputDash && dashing.lastButtonDownTime < 0 && canControl)
        dashing.lastButtonDownTime = Time.time;

    if (dashing.enabled && canControl && (Time.time - dashing.lastButtonDownTime < 0.2))
    {            
        dashing.dashing = true;
        dashing.lastButtonDownTime = -100;
        dashing.dashCounter ++;

        dashing.dashDir = Camera.mainCamera.transform.position;

        //velocity = Vector3.MoveTowards(dashing.dashDir, (dashing.dashDir + Vector3(dashing.dashDir.x + 2, 0, 0)), (2 * Time.deltaTime));

        Debug.Log("We just dashed.");            
    }       

    return velocity;

I have tried a number of different things for the dashing.dashDir, but none have worked. I know I am bascially looking to dash in the local axes of the camera?

I have also tried this:

dashing.dashDir = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));            
velocity += dashing.dashDir * dashing.baseDistance;

Any help would be greatly appreciated as this is driving me insane.

Final point. I am looking to dash the player very quickly forward in the facing direction approximately 3m and then carry to velocity when the dash finishes. Sorry if that's unclear.

Upvotes: 1

Views: 6796

Answers (2)

Shadoworker
Shadoworker

Reputation: 191

Here is my answer and it works perfectly: Moves forward, Rotates and Collides with Other objects (Having RigidBody and Box/Capsule Collider). tThis is based from Burkhard's answer.

But befor all do this : Create an empty Object set it a child of your Player and drag your camera Object into your empty Object.

NB : You can place the camera behind your player to get a best view.

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

  public class CubeControl : MonoBehaviour {

    public float speed = 10.0f;
    Rigidbody rb;
    GameObject playerEmpty;
    // Use this for initialization
    void Start () {

        rb = GetComponent<Rigidbody> ();

    }

    // Update is called once per frame
    void Update () {

        float Haxis = Input.GetAxis ("Horizontal");
        float Vaxis = Input.GetAxis ("Vertical");


        //Go Forward
        if(Input.GetKeyDown(KeyCode.UpArrow))
            {

                //Works perfectly but does no longer collide
                //rb.transform.position += transform.forward * Time.deltaTime * 10.0f;
               //Not moving in the right direction
                //rb.velocity += Vector3.forward * 5.0f;


              rb.velocity += Camera.main.transform.forward * speed;

            //rb.rotation() += new Vector3 (0.0f, headingAngle, 0.0f) * 5.0f;
            //rb.velocity += gameObject.transform.localEulerAngles * Time.deltaTime * 10.0f ; 
            }
        if(Input.GetKeyDown(KeyCode.DownArrow))
            {
                rb.velocity -= Camera.main.transform.forward * speed;
            }    
            Vector3 rotationAmount = Vector3.Lerp(Vector3.zero, new Vector3(0f, 10.0f * (Haxis < 0f ? -1f : 1f), 0f), Mathf.Abs(Haxis));
            Quaternion deltaRotation = Quaternion.Euler(rotationAmount * Time.deltaTime);
            this.transform.rotation = (this.transform.rotation * deltaRotation);

    }
}

Upvotes: 1

Imapler
Imapler

Reputation: 1422

To move an object in a along a local axis you need to use that object transform like this

dir = Camera.mainCamera.transform.forward;

If you also want to add to your current speed you need to use it like this

newVelocity = Camera.mainCamera.transform.forward * (baseSpeed + currectVelocity.magnitude);

This will set the direction to your cameras forward and add baseSpeed to your current speed.

If you want to set the distance instead of the speed you can calculate it like this

speed = distance / time;

That is the distance you want to travel and how long the dash lasts. If you do this i wouldn't add it to the current speed since that would change the distance traveled.

Upvotes: 0

Related Questions