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

How to control the direction of AddForce based on where I look with my player

Discussion in 'Scripting' started by hardhearth, Aug 22, 2015.

  1. hardhearth

    hardhearth

    Joined:
    Aug 1, 2015
    Posts:
    5
    I touch the ball, can carry it like dribbling, and unparent it with a press key where I also AddForce it. Theproblem is I cannot control the direction of the ball. I wanna make it go where I look with my player camera like shooting a ball.. Tried a couple of ways but couldn't figure out how to. I also tried "transform.forward", but this time it chooses random direction I guess based on where it looks with its position and rotation. Here is my script:

    using UnityEngine;
    using System.Collections;

    public class KickingTheBall : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    if(Input.GetKeyDown(KeyCode.E))
    {
    gameObject.GetComponent<Rigidbody>().AddForce(500, 500, 500, ForceMode.Force);
    }

    }
    }
     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Try registering your camera as a variable and taking it's transform as a reference for the force vector.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KickingTheBall : MonoBehaviour {
    5.  
    6. public Transform t_Camera; // Drag&Drop your camera over here from the inspector.
    7. private Vector3 v3_Force; // Force reference vector.
    8. public float f_Multiplier; // A multiplier value if the force wouldn't be enough.
    9.  
    10. // Use this for initialization
    11. void Start () {
    12.  
    13. }
    14.  
    15. // Update is called once per frame
    16. void Update () {
    17. if(Input.GetKeyDown(KeyCode.E))
    18. {
    19. v3_Force = t_Camera.forward;
    20. gameObject.GetComponent<Rigidbody>().AddForce(v3_Force * f_Multiplier, ForceMode.Force);
    21. }
    22.  
    23. }
    24. }
    Though I didn't test the code, but it shoulda work, if any problem occurs, just let me know so we can look into it again.

    Cheers,
    Inan
     
  3. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    In addition, it's not a good idea to reach your object's rigidbody component each time you press E. For optimization purposes, you should register it on the start, and use it whenever you need, so it'd be like :

    Code (CSharp):
    1. private Rigidbody rigid_This;
    2.  
    3. void Start()
    4. {
    5.      rigid_This = GetComponent<Rigidbody>(); // I assume the rigidbody is a component of the gameObject that this code is attached to, so no need to type gameObject.GetComponent, only typing GetComponent is the same as that.
    6. }
    And when you press E, only thing you need to do is :
    Code (CSharp):
    1. rigid_This.AddForce(.........);
     
  4. hardhearth

    hardhearth

    Joined:
    Aug 1, 2015
    Posts:
    5
    Seems I totally fail at it for now. Guess it's too much for 1 month of exp to try this stuff :). If I make the ball rigid, when I touch and make it parent it freaks out, and when I don't make it rig, I cannot add force. and also got a few other problems. So, thanks anyway dude :) Iappreciate it.