Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by VV4R_M4CHIN3 · Apr 04, 2015 at 08:58 PM · rigidbodiesdodge

Dodge Effect - Left/Right Movement

I accidentally posted this question in the forums the first time, whoops.

So I'm trying to get my player character to get a dodge effect when the player double taps "A" or "D" to avoid enemy fire. I have the double tap and everything programmed in, and it works when it happens, but the effect is lacking when the player is on the ground. When the player double taps in the air, I have my desired effect ( A quick burst of speed to the left or right ). It seems that the ground stops this from happening. I have tried to put a zero friction surface under him, but that didn't work. I will let you know, that this game is based on a rail/spline which is constantly moving the player along no matter what. Let me know if anyone has any suggestions to get the player to do this quick left/right motion while on the ground. I would appreciate it.

Here is my code so far..

 using UnityEngine;
 using System.Collections;
 
 public class PlayerDodge : MonoBehaviour {
 
 public Rigidbody rb;
 public float dodgeSpeed;
 public Vector3 dodge = new Vector3 (5, 5, 5);
 public float buttonCoolDown = 0.5f;
 public float lastTapTime = 0;
 
 
 void Start ()
 {
 GameObject player = GameObject.Find("ThirdPersonController");
 rb = player.GetComponent<Rigidbody>();
 
 lastTapTime = 0;
 
 }
 
 void Update ()
 {
 
 if (Input.GetKeyDown("d"))
 {
 if((Time.time - lastTapTime) < buttonCoolDown)
 {
 DodgeRight();
 }
 
 lastTapTime = Time.time;
 }
 
 if (Input.GetKeyDown("a"))
 {
 if((Time.time - lastTapTime) < buttonCoolDown)
 {
 DodgeLeft();
 }
 
 lastTapTime = Time.time;
 }
 
 }
 
 void DodgeRight()
 {
 rb.AddForce(dodge * dodgeSpeed );
 }
 
 void DodgeLeft ()
 {
 rb.AddForce(-dodge * dodgeSpeed );
 }
 
 }

I have noticed that the player may be colliding with the ground. My question to that is, can I avoid the ground by using the code I'm currently using? Or is there a better way to implement instead of using AddForce?

Comment
Add comment · Show 1
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 _Gkxd · Apr 05, 2015 at 05:25 AM 0
Share

You can directly set the velocity of a rigidbody ins$$anonymous$$d of using forces:

 rb.velocity = Vector3.right * dodgeSpeed;
 // Or maybe this
 rb.velocity += Vector3.right * dodgeSpeed;

I have gotten better results from setting velocities ins$$anonymous$$d of applying forces.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by moxiewhimsy · Apr 05, 2015 at 05:32 AM

If I understand correctly, your character is on rails, so whether you're on the ground or not is more important for appearance and state of actions. You could set the ground collider as a Trigger, so that it won't have any friction at all. Then use the OnTriggerStay() function to push the character out of the ground if it intersects, or rely on the rails system to bound the character out of the ground.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Fixed Joint Configuration 1 Answer

OnCollisionEnter performance 1 Answer

Why is my object tilting when moving? 2 Answers

Connect rigidbody to another moving rigidbody to simulate a hanging weight 0 Answers

Including camera angle into .Move direction 0 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