Help required, 2d mouse orbit weapon function thing:D

Ive been working with a 2d project and stumbled upon the great wall of china that i cant get over:)…
Im trying to get

this to happend but im haveing a hard time with it… so if anyone got some tips i would gladly take to them. What i got so far:

The game… movement has been pinned down and camera to,

//All the best

sebastian

You can get the position of the point under the mouse in screen space using Camera.ScreenToWorldPoint. Once you have that point, you can make the object look at it using transform.LookAt. The exact code will depend on the implementation of the game, which axis the character moves in, etc.

Thanks, ill check it out!

I have created an example project for you.

444597–15465–$CircularWeapon.rar (115 KB)

Have fun :razz:

I will submit the code here also to help other users have a quick look! :sunglasses:

using UnityEngine;
using System.Collections;

public class WeaponScript : MonoBehaviour
{
	public Transform target;				// This property is the player (only the transformation).
	public float motionSmoothing = 10f;		// How smoothly should the weapon follow the player?
	public float distanceFromTarget = 5f;	// Distance of weapon in relation to the target.
	public float angleSpeedIncrease = 5f;	// How fast the angle value should be increased?
	private float angle = 0f;				// The actual angle of the weapon in relation to 
											// the player, measured in degrees.
	
	
	void Update()
	{
		// Perform a 360 circle by increasing and resetting the angle variable.
		angle += angleSpeedIncrease * Time.deltaTime;
		Mathf.Clamp(angle, 0f, 360f); // Note: You can use an if comparison to reset
										// value back to zero but why not fully use
										// this amazing API of Unity
		
		/*
		 * In order to better understand how the final solution works, I have reversed
		 * engineered it and explaining it from simple to complex.
		 * 
		 * 1. This statement will place the weapon object in same position as player's.
		 * Vector3 targetPosition = target.position;
		 * - This is very simple, I know that.
		 * 
		 * 2. This statement will place the weapon object 5 units to the right of the player.
		 * Vector3 targetPosition = target.position + (target.right * 5f);
		 * - OK nice, but wouldn't it be better if the weapon was moving?
		 * 
		 * 3. This statement not only will place the weapon in the same position as the player
		 * but it will make it move horizonally in constant speed.
		 * Vector3 targetPosition = target.position + (target.right * Mathf.Sin(angle) * distanceFromTarget);
		 * - Wow, this is very cool, can you make it move up and down also?
		 * 
		 * 4. This statement will make the weapon move vertically.
		 * Vector3 targetPosition = target.position + (target.up * Mathf.Sin(angle) * distanceFromTarget);
		 * - OK, what if you combine these two statements, can you move it horizonally and vertically?
		 * 
		 * 5. This statement will move the weapon horizontally and vertically.
		 * Vector3 targetPosition = target.position + 
					((target.right * Mathf.Sin(angle) * distanceFromTarget) + 
					(target.up * Mathf.Sin(angle) * distanceFromTarget));
		 * - Yay... Erm... This is moving diagonally, I want it to move in circular.
		 * 
		 * 6. Oopsie... THIS statement will make the weapon move horizontally and vertically.
		 * Vector3 targetPosition = target.position + 
					(target.right * Mathf.Sin(angle) * distanceFromTarget) + 
					(target.up * Mathf.Cos(angle) * distanceFromTarget);
		 * - Yabadabadoo, but... I would like it to move in the opposite direction...
		 * 
		 * 7. Okie... But I'm getting tired :P
		 * Vector3 targetPosition = target.position + 
					(target.right * Mathf.Cos(angle) * distanceFromTarget) + 
					(target.up * Mathf.Sin(angle) * distanceFromTarget);
		 * Cool! Thanks! Can you do it also move in elastic motion? Now seems very steady,
		 * it supposed to fly around not being attached to a string.
		 * 
		 * 8. This is the final touch, it doesn't get any better, or does it?
		 */
		
		Vector3 targetPosition = target.position + 
			(target.right * Mathf.Cos(angle) * distanceFromTarget) + 
			(target.up * Mathf.Sin(angle) * distanceFromTarget);
		
		transform.position = Vector3.Lerp(transform.position, targetPosition, 
											motionSmoothing * Time.deltaTime);
		
		
	}
}

Thanks a bunch!!! Soulcon much appreciated:) i will check it out right now!

Not really what i had in mind soulcon:/ i think i made myself unclear sry! but what i ment was: The Red ball is the character and is controlled by A and D, the weapon will be controlled by the player using his mouse. Kinda like the game “SOLDAT” or “Teeworlds”, ive also tried “LookAt” scripts and follow but i cant get it to work:P guess i gotta just keep codeing until i get there:smile:

OK, now I understand. Let me have a look into it later (still 6 hours of work left :P)

Awesome Soulcon:smile:

Bump! still having problems with this^.

wow even after 4 weeks?

i think i have an idea of what you want, ive had some experiences lately about mouse movements and being centered on things n such.

Im working at different projects so im doing alot of other stuff meanwhile on other projects and just playing around in unity, but yeah would be awesome if u had some solution to my problem. =)