What i want to do is when the flashlight is on to be able to rotate the flashlight around with the mouse. But when i attach the script to the Flashlight i'm not getting any errors just nothing happen when i move the mouse around. I tried to attach the script to the GameObject i also tried to attach the script to the EthanRightHand but nothing.
But if i will attach the script to the ThirdPersonController it will rotate the character. But i want to rotate only the flashlight when it's on or to make it maybe nicer to rotate the EthanRightHand when the flashlight is on.
I can make the flashlight in the second script to be public static. But that is not the point. The problem is the first script the ObjectRotator only work on the ThirdPersonController.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectRotator : MonoBehaviour
{
int speed;
float friction;
float lerpSpeed;
private float xDeg;
private float yDeg;
private Quaternion fromRotation;
private Quaternion toRotation;
// Use this for initialization
void Start()
{
speed = 3;
friction = 3f;
lerpSpeed = 3f;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
xDeg -= Input.GetAxis("Mouse X") * speed * friction;
yDeg += Input.GetAxis("Mouse Y") * speed * friction;
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg, xDeg, 0);
transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);
}
}
}
And the flashlight script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class Flashlight : MonoBehaviour
{
[SerializeField]
Transform someBone;
Light flashlight;
// Use this for initialization
void Start()
{
flashlight = GameObject.FindGameObjectWithTag("Flashlight").GetComponent<Light>();
transform.parent = someBone;
}
// Update is called once per frame
void Update()
{
transform.localRotation = someBone.localRotation;
transform.localScale = someBone.localScale;
if (Input.GetKeyDown(KeyCode.F))
{
if (flashlight.enabled)
{
flashlight.enabled = false;
}
else
{
flashlight.enabled = true;
}
}
}
}
If you are going to make your FlashLight a child of the Hand you would need to remove these two lines from your code
transform.localRotation = someBone.localRotation;
transform.localScale = someBone.localScale;
Since that will rotate and scale the FlashLight as well as the hand.
Related
![This is how my Inspector looks like](inspector image)
And here's the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float speed = 10f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector2 Movement = Vector2.zero;
Movement.y = v;
Movement.x = h;
transform.Translate(Movement * speed * Time.deltaTime);
}
} // class
And no, increasing the speed to check if its just not moving really slow, did not help :(
You need to set correct value in inspector or in start funciton.
void Start()
{
speed = 10f;
}
inspector image
I'm learning Unity and am following a guide. In the game you have to fly a rocket through an obstacle course. This is my code for the movement of the rocket:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
Rigidbody rb;
[SerializeField] float mainThrust = 100f;
[SerializeField] float rotationSpeed = 50f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
ProcessThrust();
ProcessRotation();
}
void ProcessThrust()
{
if (Input.GetKey(KeyCode.Space))
{
//Boost
rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
}
}
void ProcessRotation()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
//Rotate left
ApplyRotate(rotationSpeed);
}
else if (Input.GetKey(KeyCode.RightArrow))
{
//Rotate right
ApplyRotate(-rotationSpeed);
}
}
void ApplyRotate(float rotationPreFrame)
{
rb.freezeRotation = true; // freezing rotation so we can manually rotate
transform.Rotate(0, 0, 1 * rotationPreFrame * Time.deltaTime);
rb.freezeRotation = true; // unfreezing rotation so the physics system can take over
}
}
For some reason I can't fly the rocket, only rotate left and right. Got any idea why?
Did you check if you have a rigidbody applied on the object?
If so, check that inside Project settings > Player > Active input handling is set to BOTH.
Anyway, try to change your input system to the new one:
https://learn.unity.com/project/using-the-input-system-in-unity
I'm making a 2d open world game with physics similar to that of stardew valley, but for some reason there's a lot of input lag on the character even when I remove all the other scripts and animations.
I even tried removing most of the sprites such as houses and whatnot but it still has a lot of lag, not framerate lag, just the player movement.
I will attempt to explain: If you just lightly tap the keyboard the player moves and stops just fine, but if you hold down the button then let go (for about 5s gives the most lag, doesn't get any worse after that), the player keeps moving a little after you let go of the key.
Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementControls : MonoBehaviour {
public float speed;
private Vector2 MoveSpeed;
private Rigidbody2D Player;
void Start() {
Player = GetComponent<Rigidbody2D>();
}
void Update() {
Vector2 PlayerInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
MoveSpeed = PlayerInput.normalized * speed;
}
void FixedUpdate() {
Player.MovePosition(Player.position + MoveSpeed * Time.fixedDeltaTime);
}
}
Anyone know how to fix this?
Here's what fixed the problem to anyone else with said problem:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementControls : MonoBehaviour {
public float speed; //Change the player's speed
private Vector2 MoveSpeed; //This is what the player's speed will be set to after some math
private Rigidbody2D Player; //The player
void Start() {
Player = GetComponent<Rigidbody2D>(); //Find the player GameObject
}
void Update() {
Vector2 PlayerInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); //Get input
MoveSpeed = PlayerInput * speed; //Math
Player.MovePosition(Player.position + MoveSpeed * Time.fixedDeltaTime); //Move the player
}
}
I'm a total newbie in Unity and I'm learning/making my first game, which will be a platformer. I want to get the movement perfect first. I've added a simple script that enables the player to move and jump.
Here it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour{
public float jumpspeed = 5f;
public float speed = 5f;
Rigidbody2D rb;
GameObject character;
// Start is called before the first frame update
void Start()
{
}
void Awake(){
rb = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)){
Jump();
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += move *Time.deltaTime * speed;
}
void Jump(){
rb.AddForce(new Vector2(0f, jumpspeed), ForceMode2D.Impulse);
}
}
Now, I want the character to face in the direction in which it moves. The png is facing to the right by default.
How can I do that? Also, can I make my movement script better?
Thanks in advance!
I typically do this using code like this based off a bool.
void FlipSprite()
{
isFacingRight = !isFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
This is what I have so far.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
CharacterController control;
[SerializeField]
float moveSpeed = 5.0f;
[SerializeField]
float jumpSpeed = 20.0f;
[SerializeField]
float gravity = 1.0f;
float yVelocity = 0.0f;
// Use this for initialization
void Start () {
control = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update ()
{
Vector3 direction = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
Vector3 velocity = direction * moveSpeed;
if (control.isGrounded) {
if (Input.GetButtonDown ("Jump")) {
yVelocity += jumpSpeed;
}
} else {
yVelocity -= gravity;
}
velocity.y = yVelocity;
control.Move (velocity*Time.deltaTime);
}
}
I'm following a tutorial, and it looks like everything is the same, but the player is not moving.
If it is all same with tutorial, your problem must be about your input settings. Check your "Horizontal", "Vertical", "Jump" from Edit -> Project Settings -> Input
Then on inspector look at variables under Axis list to check them if they assigned true, maybe there is no Horizontal or Vertical variable.