• 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 Pirfsich · Feb 26, 2014 at 11:54 AM · cameraaddforceball

Roll-A-Ball: AddForce into cameras forward direction. How?

Hello Community! I desperately need your help! I want to make a Ball-Roll game. I'm a beginner with C# and Unity, but I want to learn! I have found code snippets in the Internet, but I don't know where to put them, as I am a total newbie... The Situation is the following: I want to move a ball in all 4 directions on a plane, let the camera follow it horizontally and let the Ball move "away" from the camera when pressing W. I am posting my "PlayerControl"-Script later. I can get the ball to move in all 4 directions so far. But the directions are bound to the world coordinates. Means pressing "W" makes the ball go towards the Z-axis, "D" towards the X-Axis and so on.

The problem is, I can't get the camera to turn with the A and D keys horizontally and add a Force to the ball towards the cameras forward position! I know asking for code is not the way to learn, but I can't continue without it! Is there someone who is able to get me the right code and explain me where to put that?

I hope you can help me!

Here is my playerScript so far...


 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour
 {
     public float speed;
     void FixedUpdate()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
         rigidbody.AddForce(movement * speed * Time.deltaTime);
 
     }
 }
 

-Pirfsich

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
0

Answer by Ashutosh8126 · Feb 26, 2014 at 01:23 PM

You don't need to change the rotation of camera.. On KeyInput.. I prefer to make camera look at the ball every frame.. Most simply you can use is.. cameraTransform.LookAt(ball.transform); Camera will alwayz look at the ball.. To add a force to the ball towards the camera forward direction.. You need to get camera's local forward direction.. You can add force like..

ballRigidBody.AddForce(magnitudeOfForce * cameraTransform.forward);

I hope you understand what I mean by the variable names.. If not them plz lemme know..

Comment
Add comment · Show 1 · 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 Pirfsich · Feb 26, 2014 at 03:26 PM 0
Share

Hey Ashutosh8126, thanks for your answer!! I tried it, but because I'm a total noob, I dont know what you mean by $$anonymous$$eyInput... Could you explain to me, where I have to put that code? To the camera or the ball?

avatar image
0

Answer by JeffreyD · Feb 26, 2014 at 01:31 PM

Here is a nice roll-a-ball tutorial. https://www.youtube.com/watch?v=_IONRdFLF38&list=PLX2vGYjWbI0SZQGnOC1RJl-pcxpmAYXYO

For learning unity in general look in the learn section here at the unity site. There are many videos and tutorials to watch.

The documentation area is also very good.

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 Pirfsich · Feb 26, 2014 at 03:29 PM 0
Share

This makes the ball go the way I explained it. I don't want a top-down view, I want to make the camera follow the ball close behind and rotate when the ball does. I also followed this tutorial until moving the camera.

avatar image JeffreyD · Feb 27, 2014 at 03:47 PM 0
Share

This post seems to give you the ability to follow an object with the camera. Perhaps it will give you want you're looking for. http://answers.unity3d.com/questions/254135/follow-an-object-with-camera.html

Here is another one. A more simple solution. http://answers.unity3d.com/questions/154501/rolling-ball-camera.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image

Related Questions

Super Monkey Ball type Camera? 2 Answers

AddForce to Camera 1 Answer

Ball Character Movement Contrller 2 Answers

Making a camera that follows a rigidbodied sphere. 2 Answers

How to make camera position relative to a specific target. 1 Answer


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