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 Gimil · Apr 10, 2014 at 06:43 PM · c#anglevector3 operations

Rigidbody velocity angle.

Hello, I am stuck on velocity angle. I can make rigidbody make move forward, but what i need is to make it to move in the direction where i clicked with mouse.

using UnityEngine; using System.Collections;

 public class RMBSpell : MonoBehaviour {
     public CharacterController player;
     public float speed = 10f;
     public Rigidbody effect1Moving;
     public float timer = 60;
     public float damage;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetMouseButtonDown(1))
         {
             Quaternion rot = Quaternion.FromToRotation(player.transform.position, Input.mousePosition);
             rot.x = 0f;
             rot.z = 0f;
             Rigidbody clone;
             Vector3 targetDir = Input.mousePosition - player.transform.position;
             Vector3 forward = transform.forward;
             float angle = Vector3.Angle(targetDir, forward);
             clone = Instantiate(effect1Moving, new Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z), rot) as Rigidbody;
             clone.velocity = transform.TransformDirection(Vector3.forward * speed);
         }
 
     }
 }
Comment
Add comment · Show 3
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 robertbu · Apr 10, 2014 at 06:46 PM 0
Share

How this is solved will depend highly on the mechanics of the game.

  • Orthographic vs Perspective camera?

  • Angle of the camera with respect to the world axes?

  • How you want to map the 2D mouse position into 3D space?

The big problem is on line 21. Input.mousePosition is in screen coordinates, but your player.transform.position is in world coordinates. Start by looking at Camera.ScreenToWorldPoint(). Depending on your answers to the questions, this may not be the right solution.

avatar image Gimil · Apr 10, 2014 at 07:36 PM 0
Share

camera is isometric like in h'n's games script:

 // The target we are following
 var target : Transform;
 // The distance in the x-z plane to the target
 var distance = 10.0;
 // the height we want the camera to be above the target
 var height = 5.0;
 // How much we 
 var heightDamping = 2.0;
 //var rotationDamping = 3.0;
 
 // Place the script in the Camera-Control group in the component menu
 @script AddComponent$$anonymous$$enu("Camera-Control/Smooth Follow")
 
 
 function LateUpdate () {
     // Early out if we don't have a target
     if (!target)
         return;
     
     // Calculate the current rotation angles
     //wantedRotationAngle = target.eulerAngles.y;
     wantedHeight = target.position.y + height;
         
     //currentRotationAngle = transform.eulerAngles.y;
     currentHeight = transform.position.y;
     
     // Damp the rotation around the y-axis
     //currentRotationAngle = $$anonymous$$athf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 
     // Damp the height
     currentHeight = $$anonymous$$athf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 
     // Convert the angle into a rotation
     //currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
     
     // Set the position of the camera on the x-z plane to:
     // distance meters behind the target
     transform.position = target.position;
     transform.position -=Vector3.forward * distance;
 
     // Set the height of the camera
     transform.position.y = currentHeight;
     
     // Always look at the target
     transform.LookAt (target);
 }

Camera.ScreenToWorldPoint() dont work

avatar image robertbu · Apr 10, 2014 at 07:58 PM 0
Share

I'm out of time, so I cannot give you a detailed answer. The best/usual solution for this problem is to use Unity's mathematical Plane class to construct a mathematical plane through the pivot point of your character. Then you can use the Plane's Raycast() function to find a position. Quaternion.LookRotation() will give you the rotation. Code to do this Raycast() against a Plane to find the mouse position has been posted before, so Google might be your friend. You may want to use Quaternion.Slerp() or Quaternion.RotateTowards() to soften the rotation.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by putlucky · Apr 11, 2014 at 12:06 AM

I made something similar to what you aim to achieve using raycasting. So, basically it casts a ray from "ScreenPointToRay" (the mouse position), then while the distance between the player object and the raycasthit.point (position of raycast's collision with ground) is greater than 0.01 it initiates a Lerp(linear interpolation). This is activated through the calling of the coroutine in the Update(). It just seemed less listy than your current arrangement. Let me know if this isn't what you want.

 public class RMBSpell : MonoBehaviour {
     public CharacterController player;
     public RayCastHit hit;
     private Vector3 clickPos;
  
     // Use this for initialization
     void Start () {
  
     }
  
 IEnumerator MovePlayer ()
      {
        if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) 
       {
          clickPos = hit.point;
  
  
        while (Vector3.Distance(clickPos, player.transform.position)>0.01f)
        {
  
          player.transform.position = Vector3.Lerp(player.transform.position,clickPos, Time.deltaTime * speed);
          yield return new WaitForEndOfFrame();
        }
  
       }
  
     }
 
 }
 
     // Update is called once per frame
     void Update () {
        if (Input.GetMouseButtonDown(1))
        {
          StartCoroutine(MovePlayer());
        }
  
     }
 }
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 Gimil · Apr 11, 2014 at 04:57 PM 0
Share

But it's script to move character controller? Created another C# trigger and its what i get error: Assets/Created/Scripts/R$$anonymous$$B2.cs(35,6): error CS0116: A namespace can only contain types and namespace declarations

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Encountering Gimbal Lock when using Transform.Rotate(Axis, Angle) 1 Answer

Issues calculating angle in 2D 0 Answers

How can I limit the rotational speed of a gameObject tracking my mouse? 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