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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Apply force in direction camera is facing?

Discussion in 'Scripting' started by dwill34, Dec 13, 2018.

  1. dwill34

    dwill34

    Joined:
    May 25, 2017
    Posts:
    9
    So I was following the Roll-a-ball tutorial because I wanted to learn a little about Unity but wanted to expand upon it with a few more things. One of the things I wanted to add was a way to get the camera to pivot around the player's ball which I was able to do successfully through a few other tutorials online. However, the thing with this tutorial is that since it uses a somewhat static camera, the forces never needed to be changed based off of the camera's direction if that makes any sense. So if I rotate my camera to face the opposite way it initially faces and press W, it goes in the opposite direction.
    I wanted to make it where when I press W, the force will be added in the forward direction based off the camera's rotation. The code for the player controller is:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveController : MonoBehaviour {
    6.  
    7.     private Rigidbody rb;
    8.     public float moveSpeed;
    9.     public LayerMask groundLayers;
    10.     public float jumpForce = 10;
    11.     public SphereCollider col;
    12.  
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody>();
    16.         col = GetComponent<SphereCollider>();
    17.     }
    18.  
    19.     void FixedUpdate()
    20.     {
    21.         float moveHoriz = Input.GetAxis("Horizontal");
    22.         float moveVert = Input.GetAxis("Vertical");
    23.  
    24.         Vector3 move = new Vector3(moveHoriz, 0.0f, moveVert);
    25.  
    26.         rb.AddForce (move * moveSpeed);
    27.  
    28.         if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
    29.         {
    30.             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    31.         }
    32.     }
    33.  
    34.     private bool IsGrounded()
    35.     {
    36.         return Physics.CheckCapsule(col.bounds.center, new Vector3(col.bounds.center.x, col.bounds.min.y, col.bounds.center.z), col.radius * .9f, groundLayers);
    37.     }
    38. }
    39.  
    and in case it's needed as well, the camera rotation script is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour {
    6.  
    7.     public Transform player;
    8.     private Vector3 offset;
    9.     public bool rotCamPlayer = true;
    10.     public float camRotSpeed = 5.0f;
    11.     public bool LookAtPlayer = false;
    12.  
    13.     void Start () {
    14.         offset = transform.position - player.transform.position;
    15.     }
    16.    
    17.     void LateUpdate () {
    18.         if (rotCamPlayer)
    19.         {
    20.             Quaternion camTurnAngle = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * camRotSpeed, Vector3.up);
    21.             offset = camTurnAngle * offset;
    22.         }
    23.         transform.position = player.transform.position + offset;
    24.  
    25.         if (LookAtPlayer || rotCamPlayer)
    26.         {
    27.             transform.LookAt(player);
    28.         }
    29.     }
    30. }
    31.  
    Any help would be appreciated since everything I've seen doesn't use AddForce and I'm new to this all so I'm not sure what I should be doing.
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    Look into Transform.forward
     
  3. dwill34

    dwill34

    Joined:
    May 25, 2017
    Posts:
    9
    Would it have a similar effect as if there were momentum and acceleration when going forward? Or is that something I'd have to code in myself? Main reason I've done the AddForce is because the tutorial and it has the rolling ball effect I'm looking for.