- Home /
Mouse following character
im making a third person shooter sort of a game like the last stand. im making my character now but im stuck on making my character arms hands and the gun follow when my cursor is please can anyone help?
Answer by uhahaha · Dec 19, 2010 at 03:51 AM
Your question is more or less vague. When aiming a target, does the whole body or upper body or shoulder rotate? Anyway, the following C# code will rotate the gun:
using UnityEngine; using System.Collections;
public class GunRotator : MonoBehaviour
{
public float rotSpeed = 0.01f;
public float angle = 90.0f;
void Update()
{
float dirX = Input.GetAxis ("Mouse X") * angle;
float dirY = Input.GetAxis ("Mouse Y") * angle;
transform.Rotate(dirY * Time.deltaTime * rotSpeed, dirX * Time.deltaTime * rotSpeed, 0, Space.World);
}
}
i would like the hole body to rotate thanks for the help tho.
If you attach the above code to your character, the whole body will rotate as well. However, if the whole body rotates, it will turn stiffly like a turret & won't look natural. You need to animate legs, neck, and arm(s) in your 3D modeling software and control the animations with scripting in Unity.
theres an error which says expected insert a smeicolon at the end please can you help?
I just tested the above code, and it works. (1) Which line caused the error message? (2) It's a C# code. Did you accidentally typed the code in a javascript template? (3) Does your script name match the class name?
yep i used it as a javascript sorry it works fine now thanks for helping :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613091542im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Sprint Error script? 2 Answers
Making Character Stand? 1 Answer
rotating at mouse 0 Answers
Character rotation on foot? 1 Answer