Player zooms off screen on input and throws error and crash

My code seems to be the same as Ricks

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting.FullSerializer;
using UnityEngine;




public class PlayerController : MonoBehaviour
{
    
     
    // Start is called before the first frame update
    void Start()
    {
        
    }

     
    

    // Update is called once per frame
    void Update()
    {
        float xThrow = Input.GetAxis("Horizontal");
        
        float yThrow = Input.GetAxis("Vertical");
        


        float xOffset = xThrow * Time.deltaTime;
        float newXPos = transform.localPosition.x + xOffset;
        
        transform.localPosition = new Vector3 
        (transform.localPosition.x + newXPos,
        transform.localPosition.y, 
        transform.localPosition.z);
        
    }
}

Ive tried making a float of 0.1f for the speed of the xThrow and multiplying xThrow by xThrowSpeed by Time.deltaTime and that yielded 0 change. I used a controller to see if I got a different output due to sensitivity and it returned the same results.

Hi Kontroll,

What exactly does your code do? Take a look at this piece of code. I removed the irrelevant things.

float xOffset = xThrow * Time.deltaTime;
float newXPos = transform.localPosition.x + xOffset;
        
transform.localPosition = new Vector3 (transform.localPosition.x + newXPos, …);

Remember you can also look at the lecture code changes via the link in the Resources of each lecture.

Yeah I worked that out but the site had bugged out for me and I couldnt locate my initial question to ammend yesterday. My issue was that I had still had the transform.localPosition + newXPos whereas I needed to replace transform.localPosition with newXPos. Thanks anyrate mate

You’re welcome. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms