Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by leonardoraele · Apr 15, 2014 at 12:59 AM · transformvector3rigidbody.addforcetransform.rotationtransform.lookat

How do I add force to the direction a game object is facing?

I want do apply physics force to a game object's rigidbody using Rigidbody.AddForce in the direction that another game object's transform is facing to. How do I do that?

This is a broken script as example of what I am trying to do:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody))]
 public class PushExample : MonoBehaviour {
 
     public float strength = 1.0f;
     public Transform direction = null;
 
     void Update ()
     {
         if (this.direction != null && Input.GetButtonDown("action"))
         {
             this.rigidbody.AddForce(direction.rotation * strength);
         }
     }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by robertbu · Apr 15, 2014 at 01:01 AM

Assuming you've setup your game objects so that forward of the object (direction object in this case) is the side facing positive 'z' when the rotation is (0,0,0), then you can do:

      rigidbody.AddForce(direction.forward * strength);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image leonardoraele · Apr 15, 2014 at 01:37 AM 0
Share

Wow, how simple that was! I was having headaches trying to figure it out by myself =( Thank you!

avatar image sporkflips · Mar 20, 2016 at 01:21 AM 0
Share

If I have one object facing one way and I want to move another object based on the direction the first object is facing would I just use something like Vector3 direction = new Vector3(0, 0, firstObject.transform.position.z); ? I tried it in the context of

 public void PushBackPlayer()
 {
     attackStart = Time.time;
     while(Time.time < (attackStart + attackDuration))
     {
         pcControl.Simple$$anonymous$$ove(direction * Time.deltaTime);
     }
 }

and it crashed the Unity editor for some reason.....

avatar image
0

Answer by mrck103 · Sep 11, 2017 at 10:32 AM

For me it was the Player cam, or the FPS camera. Just make a Public GameObject and put the FPS cam in it like this. ( this a script for my game its a force wand. As you hold down Left Click the force increases and when you let go the Force decrees.)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ForceThrowWand : MonoBehaviour {
 
     public float range = 100f;
     public float Times = 0.1f;
 
     public Camera fpsCam;
 
     public ParticleSystem ps;
     public float delayOnParticle = 1f;
     private float nextTimeToUse = 0f;
 
     public float force = 1f;
 
     void Update () {
         if (Times <= 0) {
             Times += Times;
         } else {
             if (Input.GetButton ("Fire1")) {
                 Shoot ();
             } else {
                 if (force <= 1) {
                     
                 } else {
                     force -= 0.1f;
                 }
             }
 
             if (Input.GetButton ("Fire1") && Time.time >= nextTimeToUse) {
                 ps.Play ();
                 nextTimeToUse = Time.time + 1f / delayOnParticle;
             }
 
             if (Input.GetButtonDown ("Fire2")) {
 
             }
         }
     }
 
     void Shoot() {
         RaycastHit hit;
         if (Physics.Raycast (fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) {
             hit.transform.GetComponent<Rigidbody> ().AddForce (fpsCam.transform.forward * force);
             force += 0.1f;
         }
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Quaternion.LookRotation() doesn't look down 2 Answers

Moving a Platform 1 Answer

Why using = isn´t fast enough for changing Vector3 values from the transform? 2 Answers

Player rotation = Camera Rotation 0 Answers

nullifying targetObject after a Smooth LookAt Transition 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges