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 Anthonyknowles · Mar 25, 2015 at 07:57 AM · mouseaddforcespheresteering

Mouse steering of physics sphere

Hi - I followed the roll - a - ball tutorial, currently WASD controls directional "addforce" movement

How would i script in C# to get my ball to look at my mouse cursor? to give you an idea my ball might have guns on it soon so id like to have an aiming system. so basically i need my ball to look at the position of the mouse and move towards or away from it ( in its direction) when W or S is pressed

here is my current playercontroller code

 using UnityEngine;
 using System.Collections;
 
 public class playercontroller : MonoBehaviour {
     public float speed;
     private int count;
     public GUIText countText;
     // Use this for initialization
     void Start () {
         count = 0;
         setcounttext ();
 
     }
 
     void FixedUpdate () 
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
 
         rigidbody.AddForce (movement * speed * Time.deltaTime);
     }
 
     void OnTriggerEnter(Collider other)
     {
     Debug.Log("trigger");
     if (other.gameObject.tag == "pickup") 
         {
             other.gameObject.SetActive(false);
             count = count + 1;
             setcounttext();
         }
     }
 
     void setcounttext()
     {
         countText.text = "Count : " + count.ToString();
     }
 
 }
 

I have tried a few scripts that i found online but to no avail - i am beginner/moderate in C# but do not understand enough about transforms to get this without help :/

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Socrates · Mar 25, 2015 at 09:01 AM

First, I highly recommend working through the Survival Shooter tutorial on the Unity Learn section. That is where the turning code below comes from.

Second, including this code should make your character turn toward the mouse cursor, assuming that your floor is tagged appropriately.

     private Rigidbody playerRigidbody;
     private int floorMask;
     private float camRayLength = 100f;    
 
     void Awake()
     {
         floorMask = LayerMask.GetMask("Floor");
         playerRigidbody = GetComponent<Rigidbody>();
     }
 
 
     void FixedUpdate()
     {
         Turning();
     }
 
     void Turning()
     {
         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         RaycastHit floorHit;
 
         if(Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
         {
             Vector3 playerToMouse = floorHit.point - transform.position;
             playerToMouse.y = 0f;
 
             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
             playerRigidbody.MoveRotation(newRotation);
         }
     }
 

Third, if you find your movement isn't going according to plan, take a look at world versus local space; meaning are you you applying force in a world space direction or based on the rotation of the character?

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 Anthonyknowles · Mar 25, 2015 at 09:07 AM 0
Share

Thanks!! - will try this and revert back asap

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Steering Wheel with mouse drag 0 Answers

How would I reduce jittering on this RigidBody2D? 1 Answer

Sphere behaves strange by jumping on diagonal Plattforms or at terrain 0 Answers

Sphere vs Capsule collider collision resolution 0 Answers

Mouse move with specific speed? 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